What is the fastest way to combine two xml files into one

后端 未结 11 1543
广开言路
广开言路 2020-11-29 01:43

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 ju

相关标签:
11条回答
  • 2020-11-29 02:00
    var doc= XDocument.Load("file1.xml");
    var doc1= XDocument.Load("file2.xml");
    doc.Root.Add(doc2.Root.Elements());
    
    0 讨论(0)
  • 2020-11-29 02:00

    In my case the main solution did not work well, the difference was that I had a List for a thousands of files when I take one element and try to merge with the first element I get OutOfMemory exception, I added an empty template with and empty row (NodeA in this case) to solve the weird problem of the memory and run smoothly.

    I save the document in other process

    XDocument xmlDocTemplate = GetXMLTemplate(); -- create an empty document with the same root and empty row element (NodeA), everything will be merge here.
    List<XElement> lstxElements = GetMyBunchOfXML();
    
    foreach (var xmlElement lstxElements)
    {
        xmlDocTemplate
            .Root
            .Descendants("NodeA")
            .LastOrDefault()
            .AddAfterSelf(xmlElement.Descendants("NodeA"));
    }
    
    0 讨论(0)
  • 2020-11-29 02:01

    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.

    0 讨论(0)
  • 2020-11-29 02:03

    If you want to use the XmlDocument, try this

     var lNode = lDoc1.ImportNode(lDoc2.DocumentElement.FirstChild, true);
     lDoc1.DocumentElement.AppendChild(lNode);
    
    0 讨论(0)
  • 2020-11-29 02:04

    This is the fastest and cleanest way to merge xml files.

    XElement xFileRoot = XElement.Load(file1.xml);
    XElement xFileChild = XElement.Load(file2.xml);
    xFileRoot.Add(xFileChild);
    xFileRoot.Save(file1.xml);
    
    0 讨论(0)
  • 2020-11-29 02:05

    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.

    0 讨论(0)
提交回复
热议问题