What is the fastest way to combine two xml files i

2020-01-26 05:09发布

If I have two string of xml1 and xml2 which both represent xml in the same format. What is the fastest way to combine these together? The format is not important, but I just want to know how can I get rid off or ?

xml1 :

<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test1</NodeB>
      <NodeB>test2</NodeB>
   </NodeA>
</AllNodes>

xm2 :

<?xml version="1.0" encoding="utf-8"?>
<AllNodes>
   <NodeA>
      <NodeB>test6</NodeB>
      <NodeB>test7</NodeB>
   </NodeA>
   <NodeA>
      <NodeB>test99</NodeB>
      <NodeB>test23</NodeB>
   </NodeA>
</AllNodes>

and have something like this :

<?xml version="1.0" encoding="utf-8"?>
    <AllNodes>
          <NodeA>
              <NodeB>test1</NodeB>
              <NodeB>test2</NodeB>
          </NodeA>
         <NodeA>
              <NodeB>test6</NodeB>
              <NodeB>test7</NodeB>
           </NodeA>
           <NodeA>
              <NodeB>test99</NodeB>
              <NodeB>test23</NodeB>
           </NodeA>
    </AllNodes>

标签: c# xml
11条回答
狗以群分
2楼-- · 2020-01-26 05:44

Best solution to me, based on Jose Basilio answer, slightly modified,

var combinedUnique = xml1.Descendants()
    .Union(xml2.Descendants());
combinedUnique.First().Save(#fullName)
查看更多
贪生不怕死
3楼-- · 2020-01-26 05:48
var doc= XDocument.Load("file1.xml");
var doc1= XDocument.Load("file2.xml");
doc.Root.Add(doc2.Root.Elements());
查看更多
贪生不怕死
4楼-- · 2020-01-26 05:50

An XSLT transformation could do it:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="pXml1" select="''" />
  <xsl:param name="pXml2" select="''" />
  <xsl:param name="pRoot" select="'root'" />

  <xsl:template match="/">
    <xsl:variable name="vXml1" select="document($pXml1)" />
    <xsl:variable name="vXml2" select="document($pXml2)" />

    <xsl:element name="{$pRoot}">
      <xsl:copy-of select="$vXml1/*/*" />
      <xsl:copy-of select="$vXml2/*/*" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Pass in the names of the files as parameters, as well as the name of the new root element.

Apply to any XML document, e.g. an empty one.

查看更多
贪生不怕死
5楼-- · 2020-01-26 05:51

If you can guarantee this format you can combine them by doing string manipulation:

  • Read the first file, keep everything before "</AllNodes>"
  • Read the second file, remove the part up to "<AllNodes>"
  • Combine those strings.

This should be the fastest way since no parsing is needed.

const string RelevantTag = "AllNodes";

string xml1 = File.ReadAllText(xmlFile1);
xml1 = xml1.Substring(0, xml.LastIndexOf("</" + RelevantTag + ">"));

string xml2 = File.ReadAllText(xmlFile2);
xml2 = xml2.Substring(xml.IndexOf("<" + RelevantTag + ">") + "<" + RelevantTag + ">".Length, xml1.Length);

File.WriteAllText(xmlFileCombined, xm1 + xml2);

That said I would always prefer the safe way to the fast way.

查看更多
▲ chillily
6楼-- · 2020-01-26 05:53

If I were doing this (using C#), I would create a class that I can deserialize this XML to (you can use xsd.exe to do this), and then loop through all the nodes in the object representing the first piece of XML and "Add" them to the AllNodes property of the object representing the second XML.

Then serialize the second class back out the XML, and it should look like your 3rd example.

查看更多
登录 后发表回答