Coder Social home page Coder Social logo

xmldifflib's Introduction

XmlDiffLib

Use this library to find the differences between two Xml files. You can also clone this repo and build it to use the console application that makes use of this library.

How to use

You can install the library as a nuget package using the nuget command line tool, or in Visual Studio, the Nuget Package Manager.

nuget install XmlDiffLib

Sample

The setup is fairly simple, however there are quite a few options that you can include when comparing documents. I'll discuss a few here.

The basic setup looks like:

var exampleA = File.ReadAllText(@"exampleA.xml");
var exampleB = File.ReadAllText(@"exampleB.xml");

var diff = new XmlDiff(exampleA, exampleB);

diff.CompareDocuments(new XmlDiffOptions());
diff.ToString();

The results are from the comparison of the files under XmlDiffLib.Tests/Resources. In general, the default options are setup for the most common Xml cases. Order doesn't matter in attributes or elements, ignores namespace prefixes, trims whitespace on comparisons, etc... One option you might be interested in is having the comparison go both ways. That way you can see if nodes may exist in one xml that are not in the other. Note: this always compares parameter one's xml to parameter two's xml by default.

Results:

{
"DiffNodeList": 
      [
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk101\"][1]/publish_date[1]",
            "Diff ID": "1",
            "Description": "Text node does not match  |  2000-10-01 => 2015-10-12",
            "Node Type": "Text",
            "Origin Line No": 7,
            "Comp Line No": 7
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk102\"][1]/genre[1]",
            "Diff ID": "2",
            "Description": "Text node does not match  |  Fantasy => Mystery",
            "Node Type": "Text",
            "Origin Line No": 16,
            "Comp Line No": 16
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk102\"][1]/publish_date[1]",
            "Diff ID": "3",
            "Description": "Text node does not match  |  2000-12-16 => 2016-02-16",
            "Node Type": "Text",
            "Origin Line No": 18,
            "Comp Line No": 18
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk103\"][1]/price[1]",
            "Diff ID": "4",
            "Description": "Text node does not match  |  5.95 => 12.95",
            "Node Type": "Text",
            "Origin Line No": 29,
            "Comp Line No": 29
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk103\"][1]/publish_date[1]",
            "Diff ID": "5",
            "Description": "Text node does not match  |  2000-11-17 => 2016-08-17",
            "Node Type": "Text",
            "Origin Line No": 30,
            "Comp Line No": 30
         }
      ]
}

If you want compare two Xml documents and see which nodes are in parameter two's xml that are not in parameter one, you can just add the property TwoWayMatch = true to the XmlDiffOptions object.

var exampleA = File.ReadAllText(@"C:\Users\KTaborn\Documents\repos\XmlDiffLib\XmlDiffLib.Tests\Resources\exampleA.xml");
var exampleB = File.ReadAllText(@"C:\Users\KTaborn\Documents\repos\XmlDiffLib\XmlDiffLib.Tests\Resources\exampleB.xml");

var diff = new XmlDiff(exampleA, exampleB);

diff.CompareDocuments(new XmlDiffOptions()
{
  TwoWayMatch = true
});

Results:

{
"DiffNodeList": 
      [
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk101\"][1]/publish_date[1]",
            "Diff ID": "1",
            "Description": "Text node does not match  |  2000-10-01 => 2015-10-12",
            "Node Type": "Text",
            "Origin Line No": 7,
            "Comp Line No": 7
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk102\"][1]/genre[1]",
            "Diff ID": "2",
            "Description": "Text node does not match  |  Fantasy => Mystery",
            "Node Type": "Text",
            "Origin Line No": 16,
            "Comp Line No": 16
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk102\"][1]/publish_date[1]",
            "Diff ID": "3",
            "Description": "Text node does not match  |  2000-12-16 => 2016-02-16",
            "Node Type": "Text",
            "Origin Line No": 18,
            "Comp Line No": 18
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk103\"][1]/price[1]",
            "Diff ID": "4",
            "Description": "Text node does not match  |  5.95 => 12.95",
            "Node Type": "Text",
            "Origin Line No": 29,
            "Comp Line No": 29
         },
         {
            "Edit": "Update",
            "XPath": "catalog[1]/book[@id=\"bk103\"][1]/publish_date[1]",
            "Diff ID": "5",
            "Description": "Text node does not match  |  2000-11-17 => 2016-08-17",
            "Node Type": "Text",
            "Origin Line No": 30,
            "Comp Line No": 30
         },
         {
            "Edit": "Insert",
            "XPath": "catalog[1]/book[@id=\"bk105\"][1]",
            "Diff ID": "2",
            "Description": "Node not found",
            "Node Type": "Tag",
            "Origin Line No": 37,
            "Comp Line No": 25,
            "Descendants": 
               [
                  {
                     "Edit": "Update",
                     "XPath": "catalog[1]/book[@id=\"bk105\"][1]",
                     "Diff ID": "1.1",
                     "Description": "No matching attribute @id = bk105",
                     "Node Type": "Node",
                     "Origin Line No": 37,
                     "Comp Line No": 25
                  }
               ]

         }
      ]
}

xmldifflib's People

Contributors

brutalsimplicity avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.