Add attribute in xml node with nant

Is there a way to add an attribute in an xml node (which I have xpath) using nant? Tried xmlpoke, but it looks like it can only update existing attributes.

thanks.

+4
source share
2 answers

XmlPoke will definitely not work, because the xpath must match something in the first place so that it can be replaced.

The only way I know this is to create my own task that will allow you to add data to the xml file. These new tasks can be built separately and added to NAnt by copying the DLL to the NAnt \ bin folder or by expanding NAnt directly from your build files

Information to get started is at <script /> Task

If you manage to make this task generic enough, it might be nice to try sending it to NAntContrib so that everyone benefits.

+3
source

I recently did something similar. This is for inserting nodes, but they need to be easily changed.

<script language="C#" prefix="test" > <references> <include name="System.Xml.dll" /> </references> <code> <![CDATA[ [TaskName("xmlinsertnode")] public class TestTask : Task { #region Private Instance Fields private string _filename; private string _xpath; private string _fragment; #endregion Private Instance Fields #region Public Instance Properties [TaskAttribute("filename", Required=true)] public string FileName { get { return _filename; } set { _filename = value; } } [TaskAttribute("xpath", Required=true)] public string XPath { get { return _xpath; } set { _xpath = value; } } [TaskAttribute("fragment", Required=true)] public string Fragment { get { return _fragment; } set { _fragment = value; } } #endregion Public Instance Properties #region Override implementation of Task protected override void ExecuteTask() { System.Xml.XmlDocument document = new System.Xml.XmlDocument(); document.Load(_filename); System.Xml.XPath.XPathNavigator navigator = document.CreateNavigator(); navigator.SelectSingleNode(_xpath).AppendChild(_fragment); document.Save(_filename); } #endregion Override implementation of Task } ]]> </code> </script> 
+3
source

All Articles