Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing two XML files & generating a third with XMLDiff in C#
    text
    copied!<p>I am trying to write a simple algorithm to read two XML files with the exact same nodes and structure but not necessarily the same data inside the child nodes and not the same order. How could I create a simple implementation for creating a third, temporary XML being the differential between the two first ones, using Microsoft's XML Diff .DLL ?</p> <p>XML Diff on MSDN:</p> <p><a href="http://msdn.microsoft.com/en-us/library/aa302294.aspx" rel="nofollow noreferrer">XML Diff and Patch Tool</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/aa302295.aspx" rel="nofollow noreferrer">XML Diff and Patch GUI Tool </a></p> <p>sample XML code of the two different XML files to compare:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Stats Date="2011-01-01"&gt; &lt;Player Rank="1"&gt; &lt;Name&gt;Sidney Crosby&lt;/Name&gt; &lt;Team&gt;PIT&lt;/Team&gt; &lt;Pos&gt;C&lt;/Pos&gt; &lt;GP&gt;39&lt;/GP&gt; &lt;G&gt;32&lt;/G&gt; &lt;A&gt;33&lt;/A&gt; &lt;PlusMinus&gt;20&lt;/PlusMinus&gt; &lt;PIM&gt;29&lt;/PIM&gt; &lt;/Player&gt; &lt;/Stats&gt; &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Stats Date="2011-01-10"&gt; &lt;Player Rank="1"&gt; &lt;Name&gt;Sidney Crosby&lt;/Name&gt; &lt;Team&gt;PIT&lt;/Team&gt; &lt;Pos&gt;C&lt;/Pos&gt; &lt;GP&gt;42&lt;/GP&gt; &lt;G&gt;35&lt;/G&gt; &lt;A&gt;34&lt;/A&gt; &lt;PlusMinus&gt;22&lt;/PlusMinus&gt; &lt;PIM&gt;30&lt;/PIM&gt; &lt;/Player&gt; &lt;/Stats&gt; </code></pre> <p>Result wanted (difference between the two)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Stats Date="2011-01-10"&gt; &lt;Player Rank="1"&gt; &lt;Name&gt;Sidney Crosby&lt;/Name&gt; &lt;Team&gt;PIT&lt;/Team&gt; &lt;Pos&gt;C&lt;/Pos&gt; &lt;GP&gt;3&lt;/GP&gt; &lt;G&gt;3&lt;/G&gt; &lt;A&gt;1&lt;/A&gt; &lt;PlusMinus&gt;2&lt;/PlusMinus&gt; &lt;PIM&gt;1&lt;/PIM&gt; &lt;/Player&gt; &lt;/Stats&gt; </code></pre> <p>In this case, I would probably use XSLT to convert the resulting XML "differential" file into a sorted HTML file, but I am not there yet. All I want to do is to display in the third XML file the difference of every numerical value of each nodes, starting from the "GP" child-node.</p> <p>C# code I have so far:</p> <pre><code>private void CompareXml(string file1, string file2) { XmlReader reader1 = XmlReader.Create(new StringReader(file1)); XmlReader reader2 = XmlReader.Create(new StringReader(file2)); string diffFile = StatsFile.XmlDiffFilename; StringBuilder differenceStringBuilder = new StringBuilder(); FileStream fs = new FileStream(diffFile, 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(); // cleaning up after we are done with the xml diff file File.Delete(diffFile); } </code></pre> <p>That's what I have so far, but the results is garbage... note that for each "Player" node, the first three childs have <em>NOT</em> to be compared... How can I implement this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload