NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

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.

Tags

CSharp XML

Related Blogs

Enumerating the values of an enum in C#
ConnectionString Switcharoo
Autostarting a Windows Service directly after install in C#
Reading A Database Schema in SQLite with C#
Resizing Images in C#

Comments

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

Regards,

Josť

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?
thanks!

Has anyone else encountered these problems?

Add A Comment

Name:


URL:


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


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. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations