简体   繁体   中英

Using C# to add an XML node to an exiting XML file

I have an XML document that I am trying to modify...

 <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="telephone1" />
    <attribute name="contactid" />
    <order attribute="fullname" descending="false" />
    <filter type = "and">
      <condition attribute="parentcustomerid" operator="eq" uiname="Tardis Communications" uitype="account" value="{BB0D0E64-C85E-E411-9405-00155D1DEA05}" />
    </filter>
  </entity>
</fetch>

What I am trying to do is insert this...

<filter type = "and">
<condition attribute="ownerid" operator= "eq-userid"/>
</filter>

in between the already existing "filter" tags. the new filter code is from another file (.txt).

I realize that this may not make sense, however, I just want to see if it is possible. If so, I can move things around after.

Here is what I tried.

private void button1_Click(object sender, EventArgs e)
{
    XmlDataDocument doc = new XmlDataDocument();
    doc.Load(@"C:\Users\jellsworth\Downloads\mySampleXML.xml");
    //XmlNode node = null;
    foreach (XmlNode node in doc.SelectNodes("//filter/condition")) 
    {
        XmlElement mapNode = doc.CreateElement("filter");
        XmlAttribute newFilter = doc.CreateAttribute("lattitude");
        newFilter.Value = @"C:\Users\jellsworth\Downloads\playFilter.txt";
        mapNode.SetAttributeNode(newFilter);

        node.InsertBefore(mapNode, node.FirstChild);
    }
}

Any guidance would be greatly appreciated.

Try using XDocument.

        // Load document
        XDocument myDoc = XDocument.Load(".\\Main.xml");

        // Select child element "entity" then select the child element of it you want which is "filter"
        XElement filterNode = myDoc.Root.Element("entity").Element("filter");

        //Example to iterate through all of the child nodes with the name condition
        foreach (var childNode in filterNode.Descendants("condition")) {
            // you could add another attribute to each of them
            childNode.SetAttributeValue("", "");
        }

        // Example element to add
        XElement newCondition = new XElement("condition");
        newCondition.SetAttributeValue("attribute", "parentcustomerid");
        newCondition.SetAttributeValue("operator", "eq");

        filterNode.Add(newCondition);
        myDoc.Save(".\\newFile.xml");

Basically, load your document with the file path as a string in

XDocument.Load("<pathToFile>");

Selecting elements and drilling down is as simple as setting a new XElement myElement = myDoc.root.Element("<Child element name>");

Now myElement will always represent that node and can be iterated through as well. To add a node, just call whatever element such as

myElement.Add(<new XElement with attributes set>);

Let me know if you need any more help with another part of it, I'd be happy to help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM