Mailing List



NerdyHearn - Blog

<< Back To All Blogs

Comparing XML files in C# Using Microsoft's Diff and Patch Tool

Tuesday, May 26th, 2009

XML comparison is no easy task, and especially when it comes to writing your own diff tool. I recently discovered that Microsoft already has a tool which does exactly what I need: Diff 2 XML files and give me back the differences.

You will first need to download the assembly here
to use the tool.

I'll go ahead and jump into some code which does a very basic comparison of 2 different xml files, and returns the difference to an XMLWriter:

// This is the namespace:
using Microsoft.XmlDiffPatch;

The code is then very simple:

System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create("C:\diff.xml");

// There are a number of available options in the XmlDiffOptions enum
XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreComments | XmlDiffOptions.IgnoreWhitespace);
diff.Compare("C:\temp1.xml", "C:\temp2.xml", true, writer);

Very easy, but very efficient, ran incredibly fast for me, and I no longer have to write an XML diff class.

Diffin' Tom Out.


CSharp XML

Related Blogs

Using MOSS and WSS SharePoint Workflow to Resize Images in an Image Library
Retrieving the SID of a user or group account using the Win32 SDK and C#
SharePoint RPC: Corrupted Binary Files


Tom said on Tuesday, July 14th, 2009 @ 8:30 AM

@don I have not experienced any issues like you have described, did you ever figure out the issue?

Tom said on Tuesday, July 14th, 2009 @ 8:30 AM

@Jose glad to hear that you both gained something useful out of the post, the tool worked very well for me.

Jose said on Tuesday, July 14th, 2009 @ 2:11 AM

Thanks a lot for this precious information!!!

Didn't know that MS already made such tool :-D



don said on Wednesday, June 3rd, 2009 @ 8:15 AM

good to see other's are using this tool, because I'm having smoe problems with it.
I'm taking the results of the diffgram, and feeding it to the xmldiffview tool, for the colorized html view of the differences. IgnoreChildOrder is ON, which is necessary for what I'm doing.

I'm feeding this some rather large complex xml files, and I'm finding that the results as viewed in the html, are munging up the xml. Child elements are placed in different parents, children are out of order, etc. These errors seem to only be appearing on the right-hand side of the view.

Has anyone else come across a problem like this, and what did you do about it?

Has anyone else encountered these problems?

Add A Comment



Email Address: (not public, used to send notifications on further comments)


Enter the text above, except for the 1st and last character:

NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers