70

As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this:

  • The first document is the source, e.g. what I want the XML document to look like. Thus the second is the one I want to find differences in and it must not contain extra nodes not in the first document.
  • Must throw an exception when too many significant differences are found, and it should be easily understood by a human glancing at the description.
  • Child element order is important, attributes can be in any order.
  • Some attributes are ignorable; specifically xsi:schemaLocation and xmlns:xsi, though I would like to be able to pass in which ones are.
  • Prefixes for namespaces must match in both attributes and elements.
  • Whitespace between elements is irrelevant.
  • Elements will either have child elements or InnerText, but not both.

While I'm scrapping something together: has anyone written such code and would it be possible to share it here?

On an aside, what would you call the first and second documents? I've been referring to them as "source" and "target", but it feels wrong since the source is what I want the target to look like, else I throw an exception.

Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
  • Can the nodes be the same but be declared in a different order? – alexmac Oct 03 '08 at 17:25
  • No, the nodes have to be in the same order. Besides being a requirement of the documents themselves, it makes differencing a bit simpler (just enumerate children and check one-to-one). – Neil C. Obremski Oct 03 '08 at 17:31
  • > attributes can be in any order Good thing, because attributes are unordered by definition. – Robert Rossney Oct 03 '08 at 18:46
  • I call the documents, "baseline" and "test". – philsquared Nov 09 '08 at 12:10
  • possible duplicate of [What is the best way to compare XML files for equality?](http://stackoverflow.com/questions/299553/what-is-the-best-way-to-compare-xml-files-for-equality) – Andrej Adamenko Aug 30 '14 at 18:54
  • https://stackoverflow.com/questions/167946/how-would-you-compare-two-xml-documents, https://stackoverflow.com/questions/14341490/programmatic-xml-diff-merge-in-c-sharp, https://stackoverflow.com/questions/14341490/programmatic-xml-diff-merge-in-c-sharp, https://stackoverflow.com/questions/299553/what-is-the-best-way-to-compare-xml-files-for-equality, https://stackoverflow.com/questions/2924350/comparing-xmldocument-for-equality-content-wise/2924439#2924439 – Ohad Schneider Aug 22 '19 at 10:25
  • 1
    Call them "actual" and "expected" (Yes, I know I'm 13 years too late). – Dawood ibn Kareem Feb 02 '21 at 01:13

13 Answers13

64

Microsoft has an XML diff API that you can use.

Unofficial NuGet: https://www.nuget.org/packages/XMLDiffPatch.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
Danimal
  • 7,672
  • 8
  • 47
  • 57
  • 2
    This is very cool! Unfortunately the ONE thing it doesn't do is allow me to ignore certain attributes. – Neil C. Obremski Oct 03 '08 at 17:49
  • I forgot to mention in my post, one of the other things I did in the XSLT was to filter out certain attributes. – runrig Oct 03 '08 at 20:32
  • Another link for the tool is here http://msdn.microsoft.com/en-gb/library/aa302294.aspx – Miguel Madero Apr 07 '11 at 06:34
  • Worked well for me, but I did encounter bugs relating to comments (it ignored comments even though `XmlDiffOptions.IgnoreComments` wasn't specified) – Ohad Schneider Aug 21 '19 at 18:51
  • 2
    Note that XML Notepad (https://github.com/microsoft/XmlNotepad) has `XMLDiff` built in, which visually displays the differences encoded in the XMLDiff diffgram. Simply open an XML and then go to *View* -> *Compare XML files*. You can even control the `XmlDiffOptions` in the options. – Ohad Schneider Aug 22 '19 at 10:27
9

I googled up a more complete list of solutions of this problem today, I am going to try one of them soon:

Cedric Dumont
  • 1,009
  • 17
  • 38
Andrej Adamenko
  • 1,650
  • 15
  • 31
  • If you want to use external API call then I suggest you to look here [XML comparator][1]. This is very handy and you can use both API or there website UI to copy paste the content which gives you all the differences. Also to add you can ignore the order of elements or nodes by setting one flag they have. Hope this helps you! [1]: https://www.jsoftwarelabs.com/jslutils/xml-comparison – DJDeveloper Mar 06 '22 at 10:11
  • https://www.simplethread.com/checking-xml-for-semantic-equivalence-in-c/ does not work well. Eg. it does not work when you change order of the attributes. – honzakuzel1989 May 24 '22 at 13:23
  • I finally chose netbike in my Net6 tests project. It looks well for my scenario. – honzakuzel1989 May 24 '22 at 13:26
8

This code doesn't satisfy all your requirements, but it's simple and I'm using for my unit tests. Attribute order doesn't matter, but element order does. Element inner text is not compared. I also ignored case when comparing attributes, but you can easily remove that.

public bool XMLCompare(XElement primary, XElement secondary)
{
    if (primary.HasAttributes) {
        if (primary.Attributes().Count() != secondary.Attributes().Count())
            return false;
        foreach (XAttribute attr in primary.Attributes()) {
            if (secondary.Attribute(attr.Name.LocalName) == null)
                return false;
            if (attr.Value.ToLower() != secondary.Attribute(attr.Name.LocalName).Value.ToLower())
                return false;
        }
    }
    if (primary.HasElements) {
        if (primary.Elements().Count() != secondary.Elements().Count())
            return false;
        for (var i = 0; i <= primary.Elements().Count() - 1; i++) {
            if (XMLCompare(primary.Elements().Skip(i).Take(1).Single(), secondary.Elements().Skip(i).Take(1).Single()) == false)
                return false;
        }
    }
    return true;
}
Chris Benard
  • 3,167
  • 2
  • 29
  • 35
Two Cents
  • 81
  • 1
  • 1
7

try XMLUnit. This library is available for both Java and .Net

Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22
6

For comparing two XML outputs in automated testing I found XNode.DeepEquals.

Compares the values of two nodes, including the values of all descendant nodes.

Usage:

var xDoc1 = XDocument.Parse(xmlString1);
var xDoc2 = XDocument.Parse(xmlString2);

bool isSame = XNode.DeepEquals(xDoc1.Document, xDoc2.Document);
//Assert.IsTrue(isSame);

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xnode.deepequals?view=netcore-2.2

Mirek
  • 4,013
  • 2
  • 32
  • 47
5

Comparing XML documents is complicated. Google for xmldiff (there's even a Microsoft solution) for some tools. I've solved this a couple of ways. I used XSLT to sort elements and attributes (because sometimes they would appear in a different order, and I didn't care about that), and filter out attributes I didn't want to compare, and then either used the XML::Diff or XML::SemanticDiff perl module, or pretty printed each document with every element and attribute on a separate line, and using Unix command line diff on the results.

runrig
  • 6,486
  • 2
  • 27
  • 44
5

https://github.com/CameronWills/FatAntelope Another alternative library to the Microsoft XML Diff API. It has a XML diffing algorithm to do an unordered comparison of two XML documents and produce an optimal matching.

It is a C# port of the X-Diff algorithm described here: http://pages.cs.wisc.edu/~yuanwang/xdiff.html

Disclaimer: I wrote it :)

cwills
  • 2,136
  • 23
  • 17
3

I am using ExamXML for comparing XML files. You can try it. The authors, A7Soft, also provide API for comparing XML files

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Alex Gulin
  • 31
  • 1
3

Another way to do this would be -

  1. Get the contents of both files into two different strings.
  2. Transform the strings using an XSLT (which will just copy everything over to two new strings). This will ensure that all spaces outside the elements are removed. This will result it two new strings.
  3. Now, just compare the two strings with each other.

This won't give you the exact location of the difference, but if you just want to know if there is a difference, this is easy to do without any third party libraries.

Do Will
  • 711
  • 1
  • 9
  • 18
  • 2
    This doesn't answer the particular question, but the concept is relevant to the problem raise in the question. My +1. – yegor256 Jan 01 '11 at 13:12
2

Not relevant for the OP since it currently ignores child order, but if you want a code only solution you can try XmlSpecificationCompare which I somewhat misguidedly developed.

Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
1

All above answers are helpful but I tried XMLUnit which look's easy to use Nuget package to check difference between two XML files, here is C# sample code

public static bool CheckXMLDifference(string xmlInput, string xmlOutput)
    {
        Diff myDiff = DiffBuilder.Compare(Input.FromString(xmlInput))
            .WithTest(Input.FromString(xmlOutput))
            .CheckForSimilar().CheckForIdentical()
            .IgnoreComments()
            .IgnoreWhitespace().NormalizeWhitespace().Build();

        if(myDiff.Differences.Count() == 0)
        {
            // when there is no difference 
            // files are identical, return true;
            return true;
        }
        else
        {
            //return false when there is 1 or more difference in file
            return false;
        }

    }

If anyone want's to test it, I have also created online tool using it, you can take a look here

https://www.minify-beautify.com/online-xml-difference

Vikas Lalwani
  • 1,041
  • 18
  • 29
0

Based @Two Cents answer and using this link XMLSorting i have created my own XmlComparer

Compare XML program

private static bool compareXML(XmlNode node, XmlNode comparenode)
    {

        if (node.Value != comparenode.Value)
            return false;

            if (node.Attributes.Count>0)
            {
                foreach (XmlAttribute parentnodeattribute in node.Attributes)
                {
                    string parentattributename = parentnodeattribute.Name;
                    string parentattributevalue = parentnodeattribute.Value;
                    if (parentattributevalue != comparenode.Attributes[parentattributename].Value)
                    {
                        return false;
                    }

                }

            }

          if(node.HasChildNodes)
            {
            sortXML(comparenode);
            if (node.ChildNodes.Count != comparenode.ChildNodes.Count)
                return false;
            for(int i=0; i<node.ChildNodes.Count;i++)
                {

                string name = node.ChildNodes[i].LocalName;
                if (compareXML(node.ChildNodes[i], comparenode.ChildNodes[i]) == false)
                    return false;
                }

            }



        return true;
    }

Sort XML program

 private static void sortXML(XmlNode documentElement)
    {
        int i = 1;
        SortAttributes(documentElement.Attributes);
        SortElements(documentElement);
        foreach (XmlNode childNode in documentElement.ChildNodes)
        {
            sortXML(childNode);

        }
    }



  private static void SortElements(XmlNode rootNode)
    {



            for(int j = 0; j < rootNode.ChildNodes.Count; j++) {
                for (int i = 1; i < rootNode.ChildNodes.Count; i++)
                {
                    if (String.Compare(rootNode.ChildNodes[i].Name, rootNode.ChildNodes[1 - 1].Name) < 0)
                    {
                        rootNode.InsertBefore(rootNode.ChildNodes[i], rootNode.ChildNodes[i - 1]);

                    }


                }
            }
           // Console.WriteLine(j++);


    }
 private static void SortAttributes(XmlAttributeCollection attribCol)
    {
        if (attribCol == null)
            return;
        bool changed = true;
        while (changed)
        {
            changed = false;
            for (int i = 1; i < attribCol.Count; i++)
        {
                if (String.Compare(attribCol[i].Name, attribCol[i - 1].Name) < 0)
                {
                    //Replace
                    attribCol.InsertBefore(attribCol[i], attribCol[i - 1]);
                    changed = true;

                }
            }
        }
    }
Chetan Mehra
  • 189
  • 1
  • 3
  • 20
  • XSLT would be a faster way to sort xml. Also, why not sort both the docs instead of using sort in the loop? – Pankaj Jaju Nov 13 '17 at 14:51
  • @PankajJaju i know xslt is faster, but i don't have any knowledge of xslt programming, also i am sorting both the docs, i am calling root element of both the docs as first node of compareXML method ` compareXML(document1.rootnode, document2.rootnode);` and sorting each node of both the docs – Chetan Mehra Nov 20 '17 at 09:03
  • See my answer for an xslt 1.0 solution to this problem. – Stephen Flynn Apr 23 '19 at 12:00
0

I solved this problem of xml comparison using XSLT 1.0 which can be used for comparing large xml files using an unordered tree comparison algorithm. https://github.com/sflynn1812/xslt-diff-turbo