.net Xml comparison for UnitTesting

I have some unit tests where I need to make sure that the XML generated by the method contains the same elements / values ​​as the expected Xml document.

I used xmlunit in Java, and although they have a .net version, it does not seem to support namespaces. Are there any alternatives inside .net for this?

So far, I can just compare 2 Xml lines and get a true / false result to tell me if they match the data contained in it, I'm happy ...

+8
xml unit-testing nunit xmlunit
source share
4 answers

I usually found that XNode.DeepEquals is enough for my needs. This is part of BCL, so no download is required.

+7
source share

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; } 
+3
source share

Something to keep in mind about MSXML XMLDiff is that if you are comparing very large XML documents, make sure your XMLDiff.Algorithm not set to "Precise", otherwise you may run out of memory. By default, its value is set to Auto, which is a safe choice, since the API will choose whether to use Precise or Fast depending on the file size, the number of differences detected, and other factors. Here is a good read for the more technically downhill:

http://treepatch.sourceforge.net/report.pdf

+1
source share

I used MS XMLDiff - this is the past, but prefer to use Beyond Compare 3 because it has a better GUI and batch processing function (doenst has a .NET API, though).

for your testing, use XNode.DeepEquals or InnerXML to compare string based views

0
source share

All Articles