给定以下xml:
<rootnode>
<childnode arg="a">Content A</childnode>
<childnode arg="b">Content A</childnode>
</rootnode>将XMLPoke与以下XPath一起使用:
rootnode/childnode[arg='b']结果(如果替换字符串为空)为:
<rootnode>
<childnode arg="a">Content A</childnode>
<childnode arg="b"></childnode>
</rootnode>当我们真正想要删除子节点本身时,子节点的内容已经被删除了。期望的结果是:
<rootnode>
<childnode arg="a">Content A</childnode>
</rootnode>必须根据子节点参数选择子节点。
发布于 2009-02-11 23:36:31
我举起我的手!这是一个典型的问错问题的案例。
问题是不能使用xmlpoke删除单个节点。Xmlpoke只能用于编辑特定节点或属性的内容。根据问题,没有一种优雅的方法可以只使用标准的Nant目标来删除子节点。It 可以使用Nant中的一些不优雅的字符串操作来完成,但是你为什么要这样做呢?
最好的方法是编写一个简单的Nant目标。这是我之前准备的一个:
using System;
using System.IO;
using System.Xml;
using NAnt.Core;
using NAnt.Core.Attributes;
namespace XmlStrip
{
[TaskName("xmlstrip")]
public class XmlStrip : Task
{
[TaskAttribute("xpath", Required = true), StringValidator(AllowEmpty = false)]
public string XPath { get; set; }
[TaskAttribute("file", Required = true)]
public FileInfo XmlFile { get; set; }
protected override void ExecuteTask()
{
string filename = XmlFile.FullName;
Log(Level.Info, "Attempting to load XML document in file '{0}'.", filename );
XmlDocument document = new XmlDocument();
document.Load(filename);
Log(Level.Info, "XML document in file '{0}' loaded successfully.", filename );
XmlNode node = document.SelectSingleNode(XPath);
if(null == node)
{
throw new BuildException(String.Format("Node not found by XPath '{0}'", XPath));
}
node.ParentNode.RemoveChild(node);
Log(Level.Info, "Attempting to save XML document to '{0}'.", filename );
document.Save(filename);
Log(Level.Info, "XML document successfully saved to '{0}'.", filename );
}
}
}将上述步骤与对NAnt.exe.config文件的修改相结合,以加载构建文件中的自定义目标和以下脚本:
<xmlstrip xpath="//rootnode/childnode[@arg = 'b']" file="target.xml" />这将从target.xml中删除带有值为b的参数arg的子节点。这才是我真正想要的!
发布于 2019-08-13 23:00:46
xmlpeek只读取值。xmlpoke仅设置值。不幸的是,nant没有xmldelete任务。
我通过在nant文件中创建一个nant <target />解决了这个问题,我可以很容易地在项目之间重用。
我选择利用nant内置的<regex />、<echo />、<include />和<call />任务。
优势:
XML
缺点:
<!-- This file should be included before being called -->
<project name="YourProject">
<target name="RemoveLineFromFile">
<loadfile
file="${delete.from.file.path}"
property="xml.file.content" />
<property
name="delete.from.file.regex"
value="(?'BEFORE'[\w\s\W]*)\<add.*key=\"AWSProfileName\".*value=\"comsec\".*\/\>(?'AFTER'[\w\s\W]*)" />
<regex
input="${xml.file.content}"
pattern="${delete.from.file.regex}" />
<echo
file="${delete.from.file.path}"
message="${BEFORE}${AFTER}"
append="false" />
</target>
</project>下面是如何包含和调用这个目标。请记住,所有参数都是全局的,必须在调用目标之前进行定义。
delete.from.file.path:要删除的文件的路径匹配要删除的正则表达式(并在组之前和之后定义)。<project name="YourProject">
<include buildfile="path\to\nant\target\RemoveLineFromFile.build"/>
<target name="OtherWork">
<property name="delete.from.file.path" value="path\to\xml\file.xml" />
<property name="delete.from.file.regex" value="(?'BEFORE'[\w\s\W]*)\<childnode.*arg="b">(.*?)\<\/childnode\>(?'AFTER'[\w\s\W]*)" />
<call target="RemoveLineFromFile" />
</target>
</project>https://stackoverflow.com/questions/537988
复制相似问题