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

后端 未结 11 1550
广开言路
广开言路 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:14

    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"));
    

提交回复
热议问题