Here are 3 quick lessons to get you started using  the DotDiff Library.

Attached Project (vs 2008) Tutorial_DotDiff1.zip (67.29 kb)

Lessons

  1. simple compare and merge
  2. Generate a Diff Gram
  3. Advanced Compare and Merge

Common Setup

Before we start any of the lessons lets go over the common set up  steps, which are as follows:

//Step 1, create an instance of the compare engine.
XmlCompare xmlCompare = new XmlCompare();

//step2, load in the documents
xmlCompare.LoadOldDocXml(File.ReadAllText("Old Xml file"));
xmlCompare.LoadNewDocXml(File.ReadAllText("New Xml file"));

//Step 3, run the compare Engine.
xmlCompare.Compare(); //yep you have just compared these documents.

From this point forward you can now:

  • Look at the Differences and merge them back into the OldXml (Lesson 1) 
  • Generate a Diff Gram AKA a Difference Graph (Lesson 2)
  • Re-Compare and other changes.

Lesson 1 

Lesson 1, is to simply compare 2 Xml files and merge a single change. start of by following the common setup, and then you can do the following:

//now lets copy of only one of the changes
DiffInstance diffInstance = xmlCompare.Differences[0];

Console.WriteLine("About to merge the following chnage: {0}, which is {1}",
    diffInstance.XPath, diffInstance.Difference.ToString());

xmlCompare.MergeNode(diffInstance); //Merge the selected change back into the oldXml doc
xmlCompare.UpdateDifferences(); //cleans up the Differences collection.

The above code, just merged the first difference into the OldXml document. That seemed simple enough

Lesson 2

Lesson 2 looks at creating a Diff Gram, you may ask what is a diff gram? Well its the Xml which only holds what is different between the New and Old Documents, it will not contain any information which both contain. making it nice and compact (helpful for updates). As with Lesson 1, follow the common setup steps and proceed with:

//Now we can Generate the Diff Gram
XmlDocument DiffDoc = xmlCompare.GenerateDifferenceGraph(); //Simple Enough

thats it, you have just created the Diff Gram, now that was not too hard :D

Lesson 3

Lesson 3 uses the advanced feature of the "IdNode", consider the following XML

<book >
  <Id>1</Id>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

note the child node called "Id", this as you can guess is the Identifier node of the Book object. If you try and compare normally with DotDiff, it cannot tell the difference between book 1 and book 2, unless you do the following.

//Step 1, create an instance of the compare engine.
XmlCompare xmlCompare = new XmlCompare();
xmlCompare.IdNode = "Id"; //Set the name of the Id Node

This small alteration to step 1 in the setup will notify DotDiff you have child nodes with the name of "Id" as the Identifiers. You can now compare as you did before (Lesson 1) and generate Diff grams as in lesson 2

I hope this sheds some light on the use of this Library.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

I have written code for open source before, but this is my first full (small) project which I have released.

Presenting, Pause for effect, DotDiff, (first day see's 12 downloads!)

If you remember the sandbox compare tool I wrote way back last year, I have neaten some of the code up added in a Diff Gram feature and released the code on codeplex. Feel free to download it today.

Please Tell me what you think of it. Laughing, yes i know about the lack of documentation, but i have included a sample interface and comments on the code.

Hope you like it.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5