I have a few unit tests where I need to make sure that XML generated by a method contains the same elements/values as an expected Xml document.
I used xmlunit in Java, and although they have a .net version it doesn't seem to support namespaces. Are there any alternatives within .net for doing this?
As long as I can just compare 2 Xml strings and get a true/false result to tell me if they match as far as the data contained is concerned I am happy...
What is XMLUnit? From the XMLUnit.org website: XMLUnit provides you with the tools to verify the XML you emit is the one you want to create. It provides helpers to validate against an XML Schema, assert the values of XPath queries or compare XML documents against expected outcomes.
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
From the left Explorer panel, right-click the first file and choose Select for Compare from the right-click menu. Then right-click the second file and choose Compare with Selected. Both the files will be opened in the main panel, side by side in inline view mode which is comfortable for comparing the differences.
I've usually found that XNode.DeepEquals is sufficient for my needs. It's part of the BCL, so no download is required.
Try Microsoft.XmlDiffPatch:
static public bool IsXmlEqual( XmlReader x1, XmlReader x2,
bool IgnoreChildOrder, bool IgnoreComments, bool IgnorePI, bool IgnoreWhitespace,
bool IgnoreNamespaces, bool IgnorePrefixes, bool IgnoreXmlDecl, bool IgnoreDtd
)
{
XmlDiffOptions options = XmlDiffOptions.None;
if (IgnoreChildOrder) options |= XmlDiffOptions.IgnoreChildOrder;
if (IgnoreComments) options |= XmlDiffOptions.IgnoreComments;
if (IgnorePI) options |= XmlDiffOptions.IgnorePI;
if (IgnoreWhitespace) options |= XmlDiffOptions.IgnoreWhitespace;
if (IgnoreNamespaces) options |= XmlDiffOptions.IgnoreNamespaces;
if (IgnorePrefixes) options |= XmlDiffOptions.IgnorePrefixes;
if (IgnoreXmlDecl) options |= XmlDiffOptions.IgnoreXmlDecl;
if (IgnoreDtd) options |= XmlDiffOptions.IgnoreDtd;
XmlDiff xmlDiff = new XmlDiff(options);
bool bequal = xmlDiff.Compare(x1, x2, null);
return bequal;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With