合并xml文件 [英] Merging of xml documents
问题描述
我遇到的有关合并XML文档的所有解决方案都无法实现我想要的.让我解释一下:
All of the solutions I have come across regarding merging XML documents do not accomplish what I desire. Let me explain:
XML文档1:
<?xml version="1.0" encoding="utf-8" ?>
<a>
<b title="Original Section">
<b title="Original Child Section"></b>
<b title="Original Child Section 2"></b>
</b>
</a>
XML文档2:
<?xml version="1.0" encoding="utf-8" ?>
<a>
<b title="New Section">
<b title="New Child Section"></b>
</b>
<b title="Original Section">
<b title="Original Child Section">
<b title="New Child For Old Section"></b>
</b>
</b>
</a>
变成这样的最终文档:
<?xml version="1.0" encoding="utf-8" ?>
<a>
<b title="Original Section">
<b title="Original Child Section">
<b title="New Child For Old Section"></b>
</b>
<b title="Original Child Section 2"></b>
</b>
<b title="New Section">
<b title="New Child Section"></b>
</b>
</a>
文档的内容相似,但是可以具有任意数量的子节点.我也想消除重复.我认为重复项是具有相同属性的元素(基于属性名称和值).有没有人看到这种实现的可行示例?我可以设想如何使用一些循环和一点递归来编写它,但是对我来说,这似乎并不是实现我想要的最好方法:)
The documents are similar in content, but can have an arbitrary number of child nodes. I also would like to eliminate duplicates. I consider duplicates being elements with the same attributes (based on attribute name and value). Has anyone seen a working example of this implementation? I can envision how I would write it using some loops and a bit of recursion, but to me, that just doesn't seem like the best way to accomplish what I want :)
预先加油,谢谢!
* 编辑 *
由于共识是必须要有循环和递归,因此,实现这一目标的最优雅,最有效的方法是什么?我想这个问题的另一个基本问题是,在迭代时比较节点的最佳方法是什么?
Since the consensus is that loops and recursion are a must, what would be the most elegant and efficient way to accomplish this? I suppose another fundamental question to this problem is what is the best way to compare the nodes as you iterate?
推荐答案
我会写一个IEqualityComparer
来指定两个节点何时匹配"-即设置标题匹配规则.
I'd write an IEqualityComparer
that specifies when two nodes are a 'match' - i.e. sets the title matching rule.
class XElementComparer : IEqualityComparer<XElement>
{
public bool Equals(XElement x, XElement y)
{
var xTitle = x.Attribute("title");
var yTitle = y.Attribute("title");
if (xTitle == null || yTitle == null) return false;
return xTitle.Value == yTitle.Value;
}
public int GetHashCode(XElement obj)
{
return base.GetHashCode();
}
}
然后编写一个递归方法来遍历您的XML,合并根据比较器匹配的节点.
And then write a recursive method to trawl through your XML, merging nodes that match according to the comparer.
private XElement Merge(XElement node1, XElement node2)
{
// trivial cases
if (node1 == null) return node2;
if (node2 == null) return node1;
var elements1 = node1.Elements();
var elements2 = node2.Elements();
// create a merged root
var result = new XElement(node1.Name, node1.Attribute("title"));
var comparer = new XElementComparer();
var mergedNodes = elements1.Union(elements2, comparer).ToList();
// for the union of the elements, insert their merge values
foreach (var title in mergedNodes)
{
var child1 = elements1.SingleOrDefault(e => comparer.Equals(e, title));
var child2 = elements2.SingleOrDefault(e => comparer.Equals(e, title));
result.Add(Merge(child1, child2));
}
return result;
}
这篇关于合并xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!