Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I hold my hands up! This is a classic case of asking the wrong question.</p> <p>The problem is that you can't use <em>xmlpoke</em> to remove a single node. <em>Xmlpoke</em> can only be used to edit the contents of a specific node or attribute. There isn't an elegant way to remove a child node as per the question using only the standard <em>Nant</em> targets. It <strong>can</strong> be done using some inelegant string manipulation using properties in Nant, but <strong>why</strong> would you want to?</p> <p>The best way to do this is to write a simple Nant target. Here's one I prepared earlier:</p> <pre><code>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 ); } } } </code></pre> <p>Combine the above with a modification to the <em>NAnt.exe.config</em> file to load the custom target and the following script in the <em>build</em> file:</p> <pre><code>&lt;xmlstrip xpath="//rootnode/childnode[@arg = 'b']" file="target.xml" /&gt; </code></pre> <p>This will remove the <em>childnode</em> with an argument <em>arg</em> with value <em>b</em> from <em>target.xml</em>. Which is what I actually wanted in the first place! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload