i'm implementing a client-server program that synchronize files between computers. At some point, when the client connects to the server, an XML file that contains the directory structure of the location that must be synchronized with the server is sent to the server. If it is not the first time when the client connects, on the server exists such a XML files so there must be found the differences between those 2 files and request from client only the ones that files that have been changed or new. My problem is... how can I find those differences between the XML's. I have 2 XML samples(one XML can have and 1Gb).
First XML (source.xml):
Second XML (changed.xml):
I found an example that uses LinQ to XML http://www.codeproject.com/Articles/45233/Diff-in-XML-files-with-LINQ, but the XML structure used there is different, and it does not help me very much. Given the fact that i can have the tag Folder and File nested inside other nodes..i really don't know how to find the differences ...
Can someone give me an idea?
Thank you!
Best Regards, Oana

Bikesh SrivastavaPosted Oct 22, 2016, 9:36 AM
Okay... I finally opted with a pure C# solution to compare the two XML files, without using the XML Diff/Patch .dll and without even needing to use XSL transforms. I will be needing XSL transforms in the next step though, to convert the Xml into HTML for viewing purposes, but I have figured an algorithm using nothing but System.Xml and System.Xml.XPath.
Here is my algorithm:
Vivek DeshmukhPosted Oct 22, 2016, 1:59 AM
chelariu oanadanielaPosted May 23, 2013, 8:16 AM
This code generate the diffram.I did that , but I asked if you could give me an idea about how to parse the diffram so that i could extract the nodes that are different(for ex.. enumerate the nodes that are added, that are deleted or changed).
Thank you.
Parveen SiwachPosted May 23, 2013, 2:58 AM
public void CompareXml(string file1, string file2, string diffFileNameWithPath)
{
XmlReader reader1 = XmlReader.Create(new StringReader(file1));
XmlReader reader2 = XmlReader.Create(new StringReader(file2));
StringBuilder differenceStringBuilder = new StringBuilder();
using (FileStream fs = new FileStream(diffFileNameWithPath, FileMode.Create))
{
XmlWriter diffGramWriter = XmlWriter.Create(fs);
XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes);
bool bIdentical = xmldiff.Compare(file1, file2, false, diffGramWriter);
diffGramWriter.Close();
}
}
Calling Code:
CompareXml(@"C:\XML\Source.xml", @"C:\XML\changed.xml", @"C:\Diff.xml");
Hope this will help you.
chelariu oanadanielaPosted May 22, 2013, 4:42 AM
First, thx for your replay.
I tried this tool but since i need the nodes that are different i would have to parse the diffgram and i really don't know how to do that..
Could you give me an idea about how to do that?
Thank you,
Oana.
Parveen SiwachPosted May 22, 2013, 1:19 AM
http://msdn.microsoft.com/en-us/library/aa302294.aspx
public void GenerateDiffGram(string originalFile, string finalFile,
XmlWriter diffGramWriter)
{
XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes);
bool bIdentical = xmldiff.Compare(originalFile, newFile, false, diffgramWriter);
diffgramWriter.Close();
}
public void PatchUp(string originalFile, String diffGramFile, String OutputFile)
{
XmlDocument sourceDoc = new XmlDocument(new NameTable());
sourceDoc.Load(originalFile);
XmlTextReader diffgramReader = new XmlTextReader(diffGramFile);
xmlpatch.Patch(sourceDoc,diffgramReader);
XmlTextWriter output = new XmlTextWriter(OutputFile,Encoding.Unicode);
sourceDoc.Save(output);
output.Close();
}