是否有一个简单的方法可以比较两个XDocument是否相等,而忽略元素/属性顺序? [英] Is there an easy way to compare if 2 XDocuments are equal ignoring element/attribute order?

查看:71
本文介绍了是否有一个简单的方法可以比较两个XDocument是否相等,而忽略元素/属性顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对序列化代码进行单元测试时,我发现一个失败,因为我以不同的顺序列出了属性(我只是在比较XDocument.ToString()值),虽然我可以解决这个问题,但对我来说真的没关系元素或属性以什么顺序显示,只要它们在正确的层次结构中都以正确的名称存在.我可能可以编写一个方法来执行此操作,但是我想知道是否存在一种我不知道的简便构建方式.

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.

推荐答案

XNode具有DeepEquals函数,应该可以解决问题.

XNode has a DeepEquals function that should do the trick.

http://msdn.microsoft. com/en-us/library/system.xml.linq.xnode.deepequals.aspx

更新:

DeepEquals函数似乎并不总是正常工作.您可能最好实施自己的比较例程.

It appears that the DeepEquals function doesn't always work correctly. You may be best off implementing your own comparison routine.

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID = 400469

这篇关于是否有一个简单的方法可以比较两个XDocument是否相等,而忽略元素/属性顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆