比较的XElement对象的最佳方式 [英] Best way to compare XElement objects

查看:152
本文介绍了比较的XElement对象的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中,我比较有一个我期待一个的XElement 对象。我使用的方法是调用的ToString()的XElement 对象,并用硬$ C进行比较$ CD字符串值。这种方法被证明是相当不舒服,因为我总是要讲究对字符串中的格式。

in a unit test I'm comparing an XElement object with the one I expect. The method I use is to invoke .ToString() on the XElement object and compare it with a hard-coded string value. This method turned out to be quite uncomfortable since I always have to pay attention on the formatting in the string.

我检查了XElement.DeepEquals()方法,但由于某种原因它并不能帮助。

I checked out the XElement.DeepEquals() method but for any reason it doesn't help.

有没有人有一个想法是什么,我应该用最好的方法是什么?

Does anyone has an idea what is the best method I should use?

推荐答案

我发现<一href="http://blogs.msdn.com/b/ericwhite/archive/2009/01/28/equality-semantics-of-linq-to-xml-trees.aspx">this优秀文章有用。它包含实现替代 XNode.DeepEquals 的比较正常化,这使得非语义内容无关之前的XML树code样本。

I found this excellent article useful. It contains a code sample that implements an alternative to XNode.DeepEquals that normalises the XML trees before comparison which makes non-semantic content irrelevant.

要说明这一点,实施 XNode.DeepEquals 返回false这些语义等价文件:

To illustrate, the implementation of XNode.DeepEquals returns false for these semantically-equivalent documents:

XElement root1 = XElement.Parse("<Root a='1' b='2'><Child>1</Child></Root>");
XElement root2 = XElement.Parse("<Root b='2' a='1'><Child>1</Child></Root>");

但是,使用 DeepEqualsWithNormalization 的实施从这篇文章,你会得到价值,因为排序的属性不被认为是显著。

However, using the implementation of DeepEqualsWithNormalization from the article, you'll get the value true because the ordering of attributes is not considered significant.

这篇关于比较的XElement对象的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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