Comparing XML Strings in Unit Tests

Comparing two XML strings is painful.  So of course, my current project required me to come up with a way to do it in .NET.  I could only use version 2.0 of the framework, and I didn’t want to add more dependencies to solution that already has plenty (which ruled out XML Diff and Patch).  So far, I’ve come up with the following bit of code:

The validationXml contains a string representation of the XML being validated against.  It also means I only have to create one instance of XmlDocument.  After creating an XPathNavigator on the XmlDocument being compared,  an XPathExpression for the subset of XmlDocument being validated, and an XPathIterator, it can be called.

The “params” keyword makes the last argument optional, so it can contain zero or more names of XML elements to ignore when deciding whether or not to call an Assert.  I’m still figuring out how to optimize this, but I think it’s a good start.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.