11

I know that there has been a lot of questions like this but I couldn't find a reply that would satisfy my needs. I have to write an application that will compare XML files: there will be 2 types of compare, first for 2 files, listing all the differences and second one for multiple XML files listing all the variations from averages.

I am looking for some kind of class, library or API that will help me finish this task. Can you suggest some solutions ?

And yet, I do not know if I should use DOM or Xpath. Any suggestions ?

EDIT:

Ok so I have been trying to accomplish this task with XmlDiff tool but this is quite problematic to solve this for multiple Xml files - I have no idea how can I use this XmlDiffDiagram to sort out the differences among for instance 50 Xml files.

Is it going to be better with LINQ ?

0

3 Answers 3

22

Microsoft's XML Diff and Patch API should work nicely:

public void GenerateDiffGram(string originalFile, string finalFile,
                       XmlWriter diffGramWriter)
    {
        XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                      XmlDiffOptions.IgnoreNamespaces |
                                      XmlDiffOptions.IgnorePrefixes);
        bool bIdentical = xmldiff.Compare(originalFile, finalFile, false, diffGramWriter);
        diffGramWriter.Close();
    }

If you need to, you can also use the Patch tool to compare the files and merge them:

public void PatchUp(string originalFile, string diffGramFile, string outputFile)
    {
        XmlDocument sourceDoc = new XmlDocument(new NameTable());
        sourceDoc.Load(originalFile);

        using (var reader = XmlReader.Create(diffGramFile))
        {
            XmlPatch xmlPatch = new XmlPatch();
            xmlPatch.Patch(sourceDoc, reader);

            using (var writer = XmlWriter.Create(outputFile))
            {
                sourceDoc.Save(writer);
                writer.Close();
            }
            reader.Close();
        }
    }
7
  • Ok, so I have read some articles about it and some documentation but I think that this is a good solution for comparing 2 documents. What about comparing a lot of them and them summarizing (reporting) the differences ?
    – Patryk
    Nov 12, 2011 at 12:47
  • How do you plan on displaying that? Sounds like it will require quite a bit of work. Nov 12, 2011 at 18:28
  • The task is to perform 1)two xml files comparison and 2) multiple files comparison. In 2) I have to display for example ranges of numbers (maybe average) and maybe variations from average (if for instance there will be the same value in all files except one) and variations in strings (I do not yet know how to do it). The user will be able to select XMLs from database to be compared.
    – Patryk
    Nov 13, 2011 at 7:54
  • -1: I'm sure this is a copy/pasted example, but could you please 1) use XmlReader.Create instead of new XmlTextWriter, and 2) put using blocks around those XmlReader instances. I will then upvote. Nov 27, 2011 at 15:41
  • the download link on microsoft website doesn't work :(
    – Niloofar
    Dec 19, 2017 at 15:10
7

If you want just to compare XML's and you don't need to get what is difference, you can use XNode.DeepEquals Method:

var xmlTree1 = new XElement("Root",
    new XAttribute("Att1", 1),
    new XAttribute("Att2", 2),
    new XElement("Child1", 1),
    new XElement("Child2", "some content")
);
var xmlTree2 = new XElement("Root",
    new XAttribute("Att1", 1),
    new XAttribute("Att2", 2),
    new XElement("Child1", 1),
    new XElement("Child2", "some content")
);
Console.WriteLine(XNode.DeepEquals(xmlTree1, xmlTree2));
1
  • Sadly, XNode.DeepEquals does return false if the attribute order doesn't match, try exchanging the order of Att1 and Att2 for the second tree
    – Marrt
    Aug 23, 2022 at 14:59
4

Personally, I would go with LINQ to XML. You can find a good tutorial at: http://msdn.microsoft.com/en-us/library/bb387061.aspx

6
  • I have read some info about LINQ to XML and it's quite promising. Why nobody would vote this answer up ?
    – Patryk
    Nov 11, 2011 at 16:15
  • Is the XSD the same for both files?
    – Maess
    Nov 11, 2011 at 16:26
  • Stupid question: what's XSD ?
    – Patryk
    Nov 11, 2011 at 16:30
  • 1
    is the schema of both xml files the same? An XSD is a definition of the schema. Put more simply: is the structure of each file the same?
    – Maess
    Nov 11, 2011 at 16:35
  • If by the structure you mean order of elements, attributes etc. then it's not necessary but probably it's gonna be the same.
    – Patryk
    Nov 11, 2011 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.