Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net Xml comparer for UnitTesting

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...

like image 641
SomeNinjectGuy Avatar asked May 05 '11 12:05

SomeNinjectGuy


People also ask

What is XML unit?

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.

What is XML software testing?

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.

How do I compare two XML files in Visual Studio?

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.


2 Answers

I've usually found that XNode.DeepEquals is sufficient for my needs. It's part of the BCL, so no download is required.

like image 65
Mark Seemann Avatar answered Nov 15 '22 18:11

Mark Seemann


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;
}
like image 40
Nestor Avatar answered Nov 15 '22 17:11

Nestor