C# 是否有一种简单的方法来比较 2 个 XDocument 是否相等而忽略元素/属性顺序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/875625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way to compare if 2 XDocuments are equal ignoring element/attribute order?
提问by Davy8
Unit testing my serialization code I found one failed because I had attributes listed in a different order (I'm just comparing the XDocument.ToString() values) and while I could fix that, it really doesn't matter to me in what order the elements or attributes appear as long as they're all there with the right name at the right level of hierarchy. I could probably write a method do this, but I'm wondering if there's an easy built in way I'm not aware of.
对我的序列化代码进行单元测试,我发现一个失败了,因为我的属性以不同的顺序列出(我只是比较 XDocument.ToString() 值),虽然我可以解决这个问题,但对我来说以什么顺序并不重要只要元素或属性在正确的层次结构级别都具有正确的名称,它们就会出现。我可能会写一个方法来做到这一点,但我想知道是否有一种我不知道的简单内置方式。
回答by Adam Says - Reinstate Monica
XNode has a DeepEquals function that should do the trick.
XNode 有一个 DeepEquals 函数应该可以解决这个问题。
http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.deepequals.aspx
http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.deepequals.aspx
Update:
更新:
It appears that the DeepEquals function doesn't always work correctly. You may be best off implementing your own comparison routine.
DeepEquals 函数似乎并不总是正常工作。您最好实施自己的比较例程。
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=400469
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=400469
回答by Blue Toque
回答by Aim Kai
Further to "Adam Ruth" I read through the microsoft link and this blog post is very useful..
除了“Adam Ruth”,我阅读了微软的链接,这篇博文非常有用。
Eric White: http://blogs.msdn.com/b/ericwhite/archive/2009/01/28/equality-semantics-of-linq-to-xml-trees.aspx
埃里克·怀特:http: //blogs.msdn.com/b/ericwhite/archive/2009/01/28/equality-semantics-of-linq-to-xml-trees.aspx