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.