Skip to content

Instantly share code, notes, and snippets.

@rockarts
Created November 14, 2012 23:36
Show Gist options
  • Save rockarts/4075623 to your computer and use it in GitHub Desktop.
Save rockarts/4075623 to your computer and use it in GitHub Desktop.
Compares 2 XMl documents and spits out the merged file with additions, updates and deleted nodes removed.
public void CompareTwoXmlFilesInMemory()
{
using (MemoryStream diffgram = new MemoryStream())
{
XmlTextWriter diffgramWriter = new XmlTextWriter(new StreamWriter(diffgram));
XmlDiff xmldiff =
new XmlDiff(XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreNamespaces | XmlDiffOptions.IgnorePrefixes);
bool identical = xmldiff.Compare(@"appconfig2.xml", @"appconfig3.xml", false, diffgramWriter);
Assert.IsFalse(identical);
//Resets the position to 0 so we can read.
diffgram.Position = 0;
PatchUp(@"appconfig3.xml", diffgram, "test.xml");
}
}
public void PatchUp(string originalFile, Stream diffGram, string outputFile)
{
XmlDocument sourceDoc = new XmlDocument();
sourceDoc.Load(originalFile);
XmlTextReader diffgramReader = new XmlTextReader(diffGram);
XmlPatch xmlPatch = new XmlPatch();
xmlPatch.Patch(sourceDoc, diffgramReader);
XmlTextWriter output = new XmlTextWriter(outputFile, Encoding.Unicode);
sourceDoc.PreserveWhitespace = true;
sourceDoc.Save(output);
output.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment