tags:

views:

525

answers:

2

Is there a simple way to compare two XML structures to determine if they have the same structure and data?

I have a function that returns an XmlNode and I am trying to write unit tests for it. I store the correct XML result in a file. Durring the test I load the file into an XmlDocument, locate the proper XmlNode and compare against the result of the function. A straight compare does not work (as expected) and InnerXml does not work either. I am considering removing all whitespace from InnerXml and comparing that, or writing my own compair to walk the tree, but I don't like either option much.

+5  A: 

XNode.DeepEquals. Read the caveats before using it.

CodeToGlory
The question is about `XmlDocument`/`XmlNode`, not about LINQ to XML.
Pavel Minaev
To be precise, his question is about comparing XML structures. His problem is with his approach (using XmlDocument). I agree with CodeToGlory: XNode.DeepEquals.
ebpower
A: 

If you must use XmlDocument and its supporting types, consider using Microsoft's XmlDiffPatch, which performs customizable diff-operations on XML data structures.

Steve Guidi