将两个XML文件合并为一个文件

时间:2016-09-08 12:23:09

标签: c# .net xml

我的问题与此处相同:What is the fastest way to combine two xml files into one 不幸的是,那里给出的答案效果不佳。

我有完全相同的问题。 我有两个文件:

XML1:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results>
  <Memory>
    <instance00>
      <PageFaultsPersec>
        <sample time="9/8/2016 12:02:51 PM" value="1401.112" />
        <sample time="9/8/2016 12:02:52 PM" value="242.3494" />
        <sample time="9/8/2016 12:02:53 PM" value="303.1362" />
      </PageFaultsPersec>
      <AvailableBytes>
        <sample time="9/8/2016 12:02:51 PM" value="1.116967E+09" />
        <sample time="9/8/2016 12:02:52 PM" value="1.11702E+09" />
        <sample time="9/8/2016 12:02:53 PM" value="1.11761E+09" />
      </AvailableBytes>
    </instance00>
  </Memory>
</Results>

XML2:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results>
  <Processor>
    <instance_Total>
      <PercentProcessorTime>
        <sample time="9/8/2016 12:02:51 PM" value="18.4567" />
        <sample time="9/8/2016 12:02:52 PM" value="3.032005" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentProcessorTime>
      <PercentUserTime>
        <sample time="9/8/2016 12:02:51 PM" value="17.04996" />
        <sample time="9/8/2016 12:02:52 PM" value="3.03025" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentUserTime>
      <PercentPrivilegedTime>
        <sample time="9/8/2016 12:02:51 PM" value="0.7413027" />
        <sample time="9/8/2016 12:02:52 PM" value="0" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentPrivilegedTime>
    </instance_Total>
  </Processor>
</Results>

我想得到:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Results>
  <Memory>
    <instance00>
      <PageFaultsPersec>
        <sample time="9/8/2016 12:02:51 PM" value="1401.112" />
        <sample time="9/8/2016 12:02:52 PM" value="242.3494" />
        <sample time="9/8/2016 12:02:53 PM" value="303.1362" />
      </PageFaultsPersec>
      <AvailableBytes>
        <sample time="9/8/2016 12:02:51 PM" value="1.116967E+09" />
        <sample time="9/8/2016 12:02:52 PM" value="1.11702E+09" />
        <sample time="9/8/2016 12:02:53 PM" value="1.11761E+09" />
      </AvailableBytes>
    </instance00>
  </Memory>
  <Processor>
    <instance_Total>
      <PercentProcessorTime>
        <sample time="9/8/2016 12:02:51 PM" value="18.4567" />
        <sample time="9/8/2016 12:02:52 PM" value="3.032005" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentProcessorTime>
      <PercentUserTime>
        <sample time="9/8/2016 12:02:51 PM" value="17.04996" />
        <sample time="9/8/2016 12:02:52 PM" value="3.03025" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentUserTime>
      <PercentPrivilegedTime>
        <sample time="9/8/2016 12:02:51 PM" value="0.7413027" />
        <sample time="9/8/2016 12:02:52 PM" value="0" />
        <sample time="9/8/2016 12:02:53 PM" value="0" />
      </PercentPrivilegedTime>
    </instance_Total>
  </Processor>
</Results>

正如我所说,我尝试了一些链接中给出的答案,但其中一些根本不起作用,有些只是加入两个文件而没有正确放置子元素。

XML文件是磁盘上的真实文件,而不仅仅是字符串。

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

var one = XElement.Load("fileOne.xml");
var two = XElement.Load("fileTwo.xml");
one.Add(two.FirstNode);
one.Save("combined.xml");