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条回答
Explosion°爆炸
2楼-- · 2020-01-26 05:27

The easiest way to do this is using LINQ to XML. You can use either Union or Concat depending on your needs.

var xml1 = XDocument.Load("file1.xml");
var xml2 = XDocument.Load("file2.xml");

//Combine and remove duplicates
var combinedUnique = xml1.Descendants("AllNodes")
                          .Union(xml2.Descendants("AllNodes"));

//Combine and keep duplicates
var combinedWithDups = xml1.Descendants("AllNodes")
                           .Concat(xml2.Descendants("AllNodes"));
查看更多
手持菜刀,她持情操
3楼-- · 2020-01-26 05:31

Since you asked for the fastest:

If (and only if) the xml structure is always consistent: (this is pseudo code)

string xml1 = //get xml1 somehow
string xml2 = //get xml2 somehow
xml1 = replace(xml1, "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
xml1 = replace(xml1, "<allnodes>", "");
xml1 = replace(xml1, "</allnodes>", "");
xml2 = replace(xml2, "<allnodes>", "<allnodes>\n" + xml1);

It's a giant hack but it's fast. Expect to see it on TheDailyWTF when your colleagues find it.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-01-26 05:31

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"));
}
查看更多
SAY GOODBYE
5楼-- · 2020-01-26 05:40

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);
查看更多
老娘就宠你
6楼-- · 2020-01-26 05:40

You have two basic options:

  1. Parse the xml, combine the data structures, serialize back to xml.

  2. If you know the structure, use some basic string manipulation to hack it. For example, in the example above you could take the inside of allnodes in the two xml blocks and put them in a single allnodes block and be done.

查看更多
We Are One
7楼-- · 2020-01-26 05:42

If you want to use the XmlDocument, try this

 var lNode = lDoc1.ImportNode(lDoc2.DocumentElement.FirstChild, true);
 lDoc1.DocumentElement.AppendChild(lNode);
查看更多
登录 后发表回答