Inserting XML nodes and inner nodes to an existing XML document in C#

懵懂的女人 提交于 2019-12-12 06:01:58

问题


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]


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34282081/inserting-xml-nodes-and-inner-nodes-to-an-existing-xml-document-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!