views:

5871

answers:

5

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:

  • 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.

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.

+17  A: 

Microsoft has an XML diff API that you can use

Danimal
This is very cool! Unfortunately the ONE thing it doesn't do is allow me to ignore certain attributes.
Neil C. Obremski
I forgot to mention in my post, one of the other things I did in the XSLT was to filter out certain attributes.
runrig
Hi everyone, today I have the same need for xml comparision. Very happy to find the answer here. I wondering how to start using XmlDiff class: what dll files for references of XmlDiff? I find out in their sample codes, we need to use C:\Windows\assembly\GAC\XmlDiffPatch\1.0.8.28__b03f5f7f11d50a3a\XmlDiffPatch.dll and the namespace is Microsoft.XmlDiffPatch.It's quite strange that there are no getting started instruction on Microsoft 's XmlDiff home page.Do we have to find ourselves the above dll file or am I misunderstanding something? Please discuss.
Nam Gi VU
+2  A: 

Comparing XML documents is complicated. Google for xmldiff (there's even a Microsoft solution) for some tools. I've solved this a couple of ways. I used XSLT to sort elements and attributes, and filter out attributes I didn't want to compare, and then either used the XML::Diff perl module, or pretty printed each document with every element and attribute on a separate line, and using Unix command line diff on the results.

runrig
+1  A: 

I know you were really asking for code, but I just had to add that the Compare application for Notepad++ has the best functionality for comparing xml documents on the market. An absolutely brilliant use of color makes it extremely easy to pinpoint changes.

+1  A: 

Hello, I am using ExamXML for comparing XML files. You can try it.

Alex Gulin
A: 

Another way to do this would be -

  1. Get the contents of both files into two different strings.
  2. Transform the strings using an XSLT (which will just copy everything over to two new strings). This will ensure that all spaces outside the elements are removed. This will result it two new strings.
  3. Now, just compare the two strings with each other.

This won't give you the exact location of the difference, but if you just want to know if there is a difference, this is easy to do without any third party libraries.

E Thomas
-1: this doesn't answer the question.
John Saunders