Question

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 compare to walk the tree, but I don't like either option much.

Was it helpful?

Solution

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

OTHER TIPS

XNode.DeepEquals. Read the caveats before using it.

Like CodeToGlory answered, XNode.DeepEquals() might fit your bill, check the remarks section on the MSDN page.

If you are stuck with XmlDocument (instead of XDocument), the answer is: No, there is no simple (existing way) to do it. XmlNode does not override Equals(), or provide an alternative. But it is not impossible to write, and that same Remarks section can be used as a starting point for a tree-walk algorithm.

Do get a clear picture of your requirements first, concerning Attributes, comments, CDATA nodes etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top