dbones notes
its a geek thing

DotDiff - a Quick Intro to the library

January 11, 2009 20:37 by Dave

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

Related posts

Comments

January 31. 2009 13:49

Hoyt Nelson

Very nice, Dave. I am just beginning a "midnight project" at work (meaning, and off-budget, off-project plan, at my own initiative) to diff XML that our application uses for setup. DotDiff will give me a good leg up. Thanks for your work, and for putting it out there as open source!

I want to set up a blog, by the way, and looked at BlogEngine.NET -- but there are few links (that I could find) from the BlogEngine.NET site to working examples. Your looks great! Is it easy to post code, and get the syntax coloring? Or do you need to use some plug-in or special formatter?

Nice colors, by the way, on the syntax highlighting. Smile

- Hoyt

Hoyt Nelson

February 3. 2009 19:09

dave

Hello Hoyt,

Many thanks for your kind words about DotDiff. The syntax highlighing came with the blog all i have to do is sorround the code with tags, for example

[code:c#]
int i = 1;
[/code]


The rest is setup via CSS, so you can colour as you like. Let me know when you have your blog up and running, I enjoy reading a fellow coders blog.

David

dave

April 8. 2009 21:11

Franchises

Hey, nice blog theme. I have been thinking about using blogengine.net myself and am browsing around a few to check them out.

Cheers

Matthew Anderson

Franchises