复制XML元素在C#中的另一个文件 [英] Copy Xml element to another document in C#

查看:283
本文介绍了复制XML元素在C#中的另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索很多地方,看过很多例子,但我仍然无法节点我想要的地方添加到我的XML。

I've searched many places and seen many examples, but I'm still unable to add nodes to my XML in the places that I want.

这里是我的问题

我有一个XML文件,这将是我的程序读,用它作为我的新的XML文件的模板的目的。但正如我已经说过了,那我已经创建了才会有最普遍的定义,这样就意味着我需要阅读该模板之一especific节点,将其添加到新的XMLXML模板,创建新节点并为新的XML文件

I have an XML file, which will be read by my program, for the purpose of using it as a template for my new XML file. But as I've said, that "XML template" that I've created will only have the most general definitions, so that means that I'll need to read one especific node of that template, add it to the new xml, create new nodes and them to the new xml file

模板XML:

<A>
  <B>
    <c>element 1</c>
    <d>element 2</d>
    <e>element 3</e>
  </B>
  <B>
    <c>element 4</c>
    <d>element 5</d>
    <e>element 6</e>
  </B>
</A>

下面是新的文件,我需要创建:

Here's the new file that I need to create:

<A>
  <B>
    <c>element 7</c>
    <d>element 8</d>
    <e>element 9</e>
    <f>element 10</f>
    <g>element 11</g>
  </B>
<B>
    <c>element 12</c>
    <d>element 13</d>
    <e>element 14</e>
    <f>element 15</f>
    <g>element 16</g>
  </B>
</A>



正如你可以看到下面

As you can see the structure below

<A>
  <B>
    <c>element 7</c>
    <d>element 8</d>
    <e>element 9</e>
  </B>
</A>



我需要从我的模板XML复制到我的新的XML文件,(选择哪个节点启动给用户),但具体的节点将被复制到新的XML,那么我将需要一些节点添加到我复制到新的文件,使之更加完整的节点。我需要将它们添加到B的标签。

I need to copy from my template xml to my new xml file, (which node to choose is up to the user), but that specific node will be copied to the new xml, then I will need to add some nodes to the node that I've copied to the new file to make it more complete. I'll need to add them to the B tags.

我已经能够做到这一点我需要让用户持续增长的新的XML文件后,通过增加更多的模板节点和A标签之间堆叠它们。

After I've been able to do that I will need to let the user keep growing that new xml file, by adding more template nodes and stacking them between the A tags.

我已经成功地复制XML模板节点,并将其添加到新的文件,但我一直无法增加新的节点,也没有我了能够将XML越来越大,每次我的广告A B节点保持到了一个节点它赞同前一次。

I've already succeeded in copying the xml template node and adding it to the new file, but I haven't been able to add new nodes, nor have I been able to keep that xml growing, everytime I ad a B node to the A node it subscribes the one before.

如果有人知道如何帮助我,我会非常感激,因为今天是使用XML

If anyone knows how to help me I'd be very grateful, since today was my first day using XML

推荐答案

我建议使用LINQ到XML我觉得它简单,容易实现。这里
是exampple如何使用LINQ读取XML

I recomend using LINQ TO XML I think it simple and easy to implement . here is the exampple how to read xml with LInq

   XDocument xmlDoc = XDocument.Load(Server.MapPath("XMLFile.xml"));

    var persons = (from elements in xmlDoc.Descendants("A")
    where elements.Element("c").Value==//VALUE YOU LOOKING TO GET 
    select new
    {
    c = elements.Element("c").Value,
    d = elements.Element("d").Value,
    e = elements.Element("e").Value,
    }).FirstOrDefault();
    /// ADD ELEMENT TO ANOTHER XML



的XDocument xmlDoc中= XDocument.Load(服务器。在MapPath(AnotherXMLFile.xml));

XDocument xmlDoc = XDocument.Load(Server.MapPath("AnotherXMLFile.xml"));

    xmlDoc.Element("A").Add(new XElement("B", new XElement("e", persons.e)));

和这里是一个非常好的教程

and here is a very good tutorial

http://www.aspnettutorials.com/tutorials/ XML / LINQ到XML的增加,cs.aspx

这篇关于复制XML元素在C#中的另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆