## How to do XML comparison in an application?

Working on a tool that does some end-to-end testing, I have a need to compare xml input/output. Comparing xml isn’t a simple string comparison, as a single extra space would deem the results aren’t equal. I need to compare both the structure as well as the actual data contained in the xml.

This is a .NET application. I’ve looked for some components out there that perform such comparison and found some, but there’s still quite a bit of work involved in taking the results the components give me (listing each line and each difference) and showing it to the user in a meaningful way.

Instead of spending a lot of time (and therefore, a lot of my client’s money), I figured the easiest/cheapest way to implement this was to simply integrate Beyond Compare into the app. That way, it’s very, very easy to see what the differences are between the two xml documents, as Beyond Compare clearly shows the differences down to the attribute level.

The way I’m doing it is my saving the XML content to disk, and then firing up Beyond Compare using the Process class, passing along the path to the files. (see code below)

public class XmlDiffController : IXmlDiffController
{
public void OpenDiff(string leftXml, string rightXml)
{
var leftResultsXml = @"c:\temp\left-results.xml";
File.WriteAllText(leftResultsXml, leftXml);
var rightesultsXml = @"c:\temp\right-results.xml";
File.WriteAllText(rightesultsXml, rightXml);

Process.Start(new ProcessStartInfo(TesterSettings.DiffToolPath)
{
WindowStyle = ProcessWindowStyle.Maximized,
Arguments = string.Format("\"{0}\" \"\"",
leftResultsXml, rightResultsXml)
});
}
}


DiffToolPath is the path to Beyond Compare on my environment. As I’m doing this development on a Windows Virtual Machine inside a Mac host, my DiffToolPath points to Beyond Compare on my Mac, like I’ve described on my Integration of Beyond Compare and Parallels on the Mac post.

Works like a charm, quick, cheap, and easy!