什么是两个XML文件合并成一个最快的方法 [英] What is the fastest way to combine two xml files into one
本文介绍了什么是两个XML文件合并成一个最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有XML1和XML2两个字符串,它都再次以相同的格式present XML。什么是这些结合在一起的最快方法?格式并不重要,但我只是想知道我怎么能摆脱或关闭?
XML1:
<?XML版本=1.0编码=UTF-8&GT?;
<&allnodes中GT;
<&节点A GT;
<节点B GT;&TEST1 LT; /节点B GT;
<节点B GT; test2的< /节点B GT;
< /节点A>
< / allnodes中>
XM2:
<?XML版本=1.0编码=UTF-8&GT?;
<&allnodes中GT;
<&节点A GT;
<节点B GT;&TEST6 LT; /节点B GT;
<节点B GT;&TEST7 LT; /节点B GT;
< /节点A>
<&节点A GT;
<节点B GT; test99< /节点B GT;
<节点B GT; test23< /节点B GT;
< /节点A>
< / allnodes中>
和有这样的事情:
<?XML版本=1.0编码=UTF-8&GT?;
<&allnodes中GT;
<&节点A GT;
<节点B GT;&TEST1 LT; /节点B GT;
<节点B GT; test2的< /节点B GT;
< /节点A>
<&节点A GT;
<节点B GT;&TEST6 LT; /节点B GT;
<节点B GT;&TEST7 LT; /节点B GT;
< /节点A>
<&节点A GT;
<节点B GT; test99< /节点B GT;
<节点B GT; test23< /节点B GT;
< /节点A>
< / allnodes中>
解决方案
要做到这一点,最简单的方法是使用LINQ to XML。您可以使用联盟或的Concat 的根据您的需要。
VAR XML1 = XDocument.Load(file1.xml);
变种XML2 = XDocument.Load(file2.xml);//合并和删除重复
VAR combinedUnique = xml1.Descendants(allnodes中)
.Union(xml2.Descendants(allnodes中));//结合,并保持副本
VAR combinedWithDups = xml1.Descendants(allnodes中)
.Concat(xml2.Descendants(allnodes中));
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>
解决方案
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屋!
查看全文