你如何比较两个XML文档? [英] How would you compare two XML Documents?

查看:118
本文介绍了你如何比较两个XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一些广泛的单元测试的基类的一部分,我编写了一个辅助函数,递归地将一个XmlDocument对象的节点与C#(.NET)中的另一个节点进行比较。这些的一些要求:

As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this:


  • 第一个文档是我想要的XML文档看起来像。因此,第二个是我想要找到的差异,它不能包含不在第一个文档中的额外节点。

  • 必须在太多时抛出异常发现了明显的差异,并且人们应该容易理解该描述。

  • 子元素顺序很重要,属性可以是任何顺序。

  • 一些属性是可忽略的;特别是 xsi:schemaLocation xmlns:xsi ,但我想能够传递哪些。 / li>
  • 命名空间的前缀必须在属性和元素中匹配。

  • 元素之间的空格是不相关的。

  • 元素将有子元素 InnerText ,但不是两者。

  • The first document is the source, e.g. what I want the XML document to look like. Thus the second is the one I want to find differences in and it must not contain extra nodes not in the first document.
  • Must throw an exception when too many significant differences are found, and it should be easily understood by a human glancing at the description.
  • Child element order is important, attributes can be in any order.
  • Some attributes are ignorable; specifically xsi:schemaLocation and xmlns:xsi, though I would like to be able to pass in which ones are.
  • Prefixes for namespaces must match in both attributes and elements.
  • Whitespace between elements is irrelevant.
  • Elements will either have child elements or InnerText, but not both.

当我在一起报废时:有人写了这样的代码,可以在这里分享吗?

另一方面,你会把第一个和第二个文件称为什么?我一直把它们称为源和目标,但感觉不对,因为是我想要的目标看起来像,否则我抛出例外

On an aside, what would you call the first and second documents? I've been referring to them as "source" and "target", but it feels wrong since the source is what I want the target to look like, else I throw an exception.

推荐答案

Microsoft有 XML diff API 您可以使用

Microsoft has an XML diff API that you can use

这篇关于你如何比较两个XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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