将两个xml文件合并为一个的最快方法是什么 [英] What is the fastest way to combine two xml files into one
本文介绍了将两个xml文件合并为一个的最快方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有两个 xml1 和 xml2 字符串,它们都以相同的格式表示 xml.将这些组合在一起的最快方法是什么?格式不重要,但我只想知道我怎样才能摆脱或?
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>
并且有这样的东西:
<?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>
推荐答案
最简单的方法是使用 LINQ to XML.您可以使用 Union 或 Concat 取决于您的需要.
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"));
这篇关于将两个xml文件合并为一个的最快方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文