比较平等的XmlDocument(内容明智) [英] Comparing XmlDocument for equality (content wise)

查看:168
本文介绍了比较平等的XmlDocument(内容明智)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想比较一个的XmlDocument的内容,它只是这样吗?

If I want to compare the contents of a XMlDocument, is it just like this?

XmlDocument doc1 = GetDoc1();
XmlDocument doc2 = GetDoc2();

if(doc1 == doc2)
{

}

我不检查,如果他们都是同一个对象的引用,但是如果XML的内容是一样的。

I am not checking if they are both the same object reference, but if the CONTENTS of the xml are the same.

推荐答案

否。 XmlDocument的不覆盖等于的行为()所以,它实际上只是执行基准平等 - 这将在你的榜样失败,除非该文件实际上是同一个对象实例。

No. XmlDocument does not override the behavior of the Equals() method so, it is in fact just performing reference equality - which will fail in your example, unless the documents are actually the same object instance.

如果要比较的内容的文件,你将不得不(属性,元素,commments,督察等)实现自己的逻辑 要注意:。这不是小事

根据您的具体情况,您可能能够删除从文档中所有非必要的空白(这本身是需要技巧的),并比较它们所产生的XML文本。这是不完美的 - 它失败在语义上是相同的,但在不同的东西怎么样的命名空间的使用和声明的,或某些值是否被转义与否,元素的顺序,等文档。正如我之前所说的,XML的比较是不平凡的。

Depending on your exact scenario, you may be able to remove all non-essential whitespace from the document (which itself can be tricky) and them compare the resulting xml text. This is not perfect - it fails for documents that are semantically identical, but differ in things like how namespaces are used and declared, or whether certain values are escaped or not, the order of elements, and so on. As I said before, XML comparison is not trivial.

您还需要明确界定意味着什么两个XML文档是相同的。是否元素或属性订货事?请问情况下(文本节点)有关系吗?如果你忽略多余CDATA部分?不要处理指令数?什么完全合格与部分限定命名空间?

You also need to clearly define what it means for two XML documents to be "identical". Does element or attribute ordering matter? Does case (in text nodes) matter? Should you ignore superfluous CDATA sections? Do processing instructions count? What about fully qualified vs. partially qualified namespaces?

在任何通用的实现,你可能会想这两个文档转换成某种规范形式(无论是XML或者一些其他的表示),然后比较规范化的含量

In any general purpose implementation, you're likely going to want to transform both documents into some canonical form (be it XML or some other representation) and then compare the canonicalized content.

工具已经存在执行XML差分一样的Microsoft XML比较/补丁您可以利用该识别两个文件之间的差异。据我所知,这个工具是不是分布在源代码的形式......所以使用它在嵌入式应用中,你需要脚本的进程(如果你打算使用它,你应该首先确认许可条款允许它的使用和再分配。)

Tools already exist that perform XML differencing, like Microsoft XML Diff/Patch, you may be able to leverage that to identify differences between two documents. To my knowledge that tool is not distributed in source form ... so to use it in an embedded application you would need to script the process (if you plan to use it, you should first verify that the licensing terms allow it's use and redistribution).

编辑: 检查出的 @马克斯·托罗的回答如果你使用.NET 3.5 SP1,因为显然有一个在XLINQ一个选项,可能会有所帮助。很高兴知道它的存在。

Check out @Max Toro's answer if you're using .NET 3.5 SP1, as apparently there's an option in XLinq that may be helpful. Nice to know it exists.

这篇关于比较平等的XmlDocument(内容明智)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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