简体   繁体   English

将XML节点和内部节点插入C#中的现有XML文档

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

Currently I have a working C# program that works as follows: 目前,我有一个工作的C#程序,其工作方式如下:

  1. Accept .xls template with values (xls is manually created by user) 接受带有值的.xls模板(xls由用户手动创建)
  2. Save the values (matching fields) to the database 将值(匹配字段)保存到数据库
  3. Convert and write .xls to XML. 将.xls转换并写入XML。 Please see below sample output: 请参见下面的示例输出:

    Existing XML Structure 现有的XML结构

Now, what I want to do is: 现在,我想做的是:

  1. Read the existing xml (the created xml) 读取现有的xml(创建的xml)
  2. Insert another set of nodes and subnodes ( ReleaseLine and sub nodes). 插入另一组节点和子节点( ReleaseLine和子节点)。 It must accept multiple ReleaseLine. 它必须接受多个ReleaseLine。
  3. Save/create the new xml with appended nodes. 保存/创建带有附加节点的新xml。 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. 我现有的C#程序很简单,但是XML节点和层次结构很深。 I just created the C# code using new XElement method and passing values for each nodes. 我刚刚使用new XElement方法并为每个节点传递值创建了C#代码。 Then I simply use xmlDocument.Save() method to write the xml. 然后,我仅使用xmlDocument.Save()方法编写xml。

[Existing XML Program][3] [现有的XML程序] [3]

To add nodes or append content in existing xml-data I´d use Linq to XML. 要在现有的xml数据中添加节点或追加内容,请使用Linq到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: 在存在XML数据之后:

`<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! 我正面临这个问题,Linq给了我最简单的方法来实现这一目标! There are also other similar way eg here . 还有其他类似的方法,例如这里 But I tried a bit more and DescendantsAndSelf() made it easier for me to go through. 但是我尝试了更多,DescendantsAndSelf()使我更容易理解。

我找到了问题的答案,这是链接http://www.xmlplease.com/add-xml-linq。使用XPathSelectElement方法,可以找到正确的节点并附加了XElement的新块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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