Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attribute to an xml node with nant

Tags:

xml

nant

xmlpoke

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

thanks.

like image 817
Meidan Alon Avatar asked Dec 11 '08 10:12

Meidan Alon


People also ask

How do you add an element to an attribute in XML?

Get the element node and use SetAttribute to add an attribute to the attribute collection of that element. Create an XmlAttribute node using the CreateAttribute method, get the element node, then use SetAttributeNode to add the node to the attribute collection of that element.

What is attribute node in XML?

Node Types Attribute Node − Each attribute is considered an attribute node. It contains information about an element node, but is not actually considered to be children of the element. Text Node − The document texts are considered as text node.

What is XML node and XML element?

According to the XML DOM, everything in an XML document is a node: The entire document is a document node. Every XML element is an element node. The text in the XML elements are text nodes. Every attribute is an attribute node.

What is XML node set?

A node set is a set of nodes. When you write an XPath expression to return one or more nodes, you call these nodes a node set. For example, if you use the following expression to return a node called title , you will have a set of nodes all called title (assuming there's more than one record). child::title. child:: ...


2 Answers

XmlPoke will definitely not work because the xpath must match something in the first place to be able to replace it.

The only way I know of doing this is to create your own task that would allow you to add data to an xml file. These new tasks can either be build separately and added to NAnt by copying dlls into NAnt\bin folder, or by extending NAnt directly from your build files

The information to get you started is found on <script/> Task

If you happen to make this task generic enough, it might be good to try to submit it to NAntContrib so everyone benefits.

like image 109
earlNameless Avatar answered Sep 27 '22 21:09

earlNameless


I made something similar recently. This is for inserting nodes, but should 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>
like image 42
Christopher Stott Avatar answered Sep 27 '22 20:09

Christopher Stott