你会如何比较两个XML文档? [英] How would you compare two XML Documents?
问题描述
至于一些广泛的单元测试的基类的一部分,我写一个辅助函数,递归比较1 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
,不过我想应该能够传递哪些是 - prefixes命名空间必须在两个属性和元素相匹配。
- 元素之间的空白是无关紧要的。
- 元素的或者的子元素的或的
的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
andxmlns: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.
而我再杀共同的东西:有没有人写过这样code和它有可能要在这里分享
While I'm scrapping something together: has anyone written such code and would it be possible to share it here?
在顺便说一句,你会称之为第一和第二文档?我已经提到他们作为源和目标,但感觉错了,因为在源是我想在目标的样子,否则我扔一个例外。
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.
推荐答案
微软有一个 XML差异API ,您可以用
这篇关于你会如何比较两个XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!