0

Currently I have a working C# program that works as follows:

  1. Accept .xls template with values (xls is manually created by user)
  2. Save the values (matching fields) to the database
  3. Convert and write .xls to XML. Please see below sample output:

    Existing XML Structure

Now, what I want to do is:

  1. Read the existing xml (the created xml)
  2. Insert another set of nodes and subnodes (ReleaseLine and sub nodes). It must accept multiple ReleaseLine.
  3. Save/create the new xml with appended nodes. Please see below output:

    This is what I'm looking for:

My existing C# program is simple but the XML nodes and hierarchy is bloody deep. I just created the C# code using new XElement method and passing values for each nodes. Then I simply use xmlDocument.Save() method to write the xml.

[Existing XML Program][3]

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

To add nodes or append content in existing xml-data I´d use Linq to XML.

 XElement xml = XElement.Load("file.xml");

        xml.Add( new XElement("uberNode",
        new XElement("childNode", content),
        new XElement("anotherChildNode", content)));
        xml.Save("file.xml");

Here are some other related solutions.

Add to specific node (with example):

Following exisiting XML-data:

`<Names>
  <Name>
    <prename>John</prename>
    <lastname>Snow</lastname>
  </Name>
  <Name>
    <prename>Harry</prename>
    <lastname>Harry</lastname>
  </Name>
</Names>`

Now I want to add an "age"-tag before the first "prename"-tag and a "family"-tag after the first "lastname"-tag.

  XElement xml = XElement.Load("file.xml");

            var childrens = xml.DescendantsAndSelf().ToArray();
            var first_prename = childrens[2];
            var first_lastname = childrens[3];
            Console.WriteLine(childrens[0]); //prints out the whole content

            first_prename.AddBeforeSelf(new XElement("age", 22));

        first_lastname.AddAfterSelf(new XElement("family", new XElement("mother", "paula"), new XElement("father", "paul")));

        xml.Save("file.xml");

Outcome:

`<Names>
  <Name>
    <age>22</age>
    <prename>John</prename>
    <lastname>Snow</lastname>
    <family>
      <mother>paula</mother>
      <father>paul</father>
    </family>
  </Name>
  <Name>
    <prename>Harry</prename>
    <lastname>Harry</lastname>
  </Name>
</Names>`

I was facing the problem and Linq gave me the easiest way to accomplish that! There are also other similar way e.g. here. But I tried a bit more and DescendantsAndSelf() made it easier for me to go through.

Community
  • 1
  • 1
Stophane
  • 31
  • 5
  • Thanks for your quick reply. I tried the following code to insert the new sets of nodes inside the ReleaseLine node, however it was inserted to the root of XML. How do I insert the new nodes to the right subnodes? Here is my code. Thanks again for your help.
    –  Dec 15 '15 at 07:22
  • I edited my last answer. And i got to thank you too, because I had to solve this problem at the moment! – – Stophane Dec 15 '15 at 08:57
  • Works like a charm, thanks Stophano. I use xml.Descendants() method instead, but the solution you provided helps a lot, thanks again, cheers! –  Dec 16 '15 at 00:23
  • Hi Stophane, let say the elements above the node is dynamic and keep changing (which is the node), let say the array children number will not be the same as childrens[3], or it could be children[6], depending on the value of my excel spreadsheet. How do you write the code so your node is flexible and automatically adjusting underneath the node? Thanks again for your help. –  Dec 19 '15 at 06:07
  • Hi Arthur, to get you a fast possible solution if remembered the following two links. First Link give an explanation how to update existing Values and the second link show how to selectivly replace content. http://stackoverflow.com/questions/1487653/update-xml-with-c-sharp-using-linq || http://stackoverflow.com/questions/5820143/how-can-i-update-replace-an-element-of-an-xelement-from-a-string – Stophane Dec 19 '15 at 06:56
0

I found an answer to my question, here is the link http://www.xmlplease.com/add-xml-linq Using XPathSelectElement method, I was able to find the right node and appended new block of XElement.