Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Here is my algorithm:</p> <pre><code>private void CompareXml(string file1, string file2) { // Load the documents XmlDocument docXml1 = new XmlDocument(); docXml1.Load(file1); XmlDocument docXml2 = new XmlDocument(); docXml2.Load(file2); // Get a list of all player nodes XmlNodeList nodes1 = docXml1.SelectNodes("/Stats/Player"); XmlNodeList nodes2 = docXml2.SelectNodes("/Stats/Player"); // Define a single node XmlNode node1; XmlNode node2; // Get the root Xml element XmlElement root1 = docXml1.DocumentElement; XmlElement root2 = docXml2.DocumentElement; // Get a list of all player names XmlNodeList nameList1 = root1.GetElementsByTagName("Name"); XmlNodeList nameList2 = root2.GetElementsByTagName("Name"); // Get a list of all teams XmlNodeList teamList1 = root1.GetElementsByTagName("Team"); XmlNodeList teamList2 = root2.GetElementsByTagName("Team"); // Create an XmlWriterSettings object with the correct options. XmlWriter writer = null; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); settings.OmitXmlDeclaration = false; // Create the XmlWriter object and write some content. writer = XmlWriter.Create(StatsFile.XmlDiffFilename, settings); writer.WriteStartElement("StatsDiff"); // The compare algorithm bool match = false; int j = 0; try { // the list has 500 players for (int i = 0; i &lt; 500; i++) { while (j &lt; 500 &amp;&amp; match == false) { // There is a match if the player name and team are the same in both lists if (nameList1.Item(i).InnerText == nameList2.Item(j).InnerText) { if (teamList1.Item(i).InnerText == teamList2.Item(j).InnerText) { match = true; node1 = nodes1.Item(i); node2 = nodes2.Item(j); // Call to the calculator and Xml writer this.CalculateDifferential(node1, node2, writer); j = 0; } } else { j++; } } match = false; } // end Xml document writer.WriteEndElement(); writer.Flush(); } finally { if (writer != null) writer.Close(); } } </code></pre> <p>XML Results:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;StatsDiff&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;0&lt;/GP&gt; &lt;G&gt;0&lt;/G&gt; &lt;A&gt;0&lt;/A&gt; &lt;Points&gt;0&lt;/Points&gt; &lt;PlusMinus&gt;0&lt;/PlusMinus&gt; &lt;PIM&gt;0&lt;/PIM&gt; &lt;PP&gt;0&lt;/PP&gt; &lt;SH&gt;0&lt;/SH&gt; &lt;GW&gt;0&lt;/GW&gt; &lt;OT&gt;0&lt;/OT&gt; &lt;Shots&gt;0&lt;/Shots&gt; &lt;ShotPctg&gt;0&lt;/ShotPctg&gt; &lt;ShiftsPerGame&gt;0&lt;/ShiftsPerGame&gt; &lt;FOWinPctg&gt;0&lt;/FOWinPctg&gt; &lt;/Player&gt; &lt;Player Rank="2"&gt; &lt;Name&gt;Steven Stamkos&lt;/Name&gt; &lt;Team&gt;TBL&lt;/Team&gt; &lt;Pos&gt;C&lt;/Pos&gt; &lt;GP&gt;1&lt;/GP&gt; &lt;G&gt;0&lt;/G&gt; &lt;A&gt;0&lt;/A&gt; &lt;Points&gt;0&lt;/Points&gt; &lt;PlusMinus&gt;0&lt;/PlusMinus&gt; &lt;PIM&gt;2&lt;/PIM&gt; &lt;PP&gt;0&lt;/PP&gt; &lt;SH&gt;0&lt;/SH&gt; &lt;GW&gt;0&lt;/GW&gt; &lt;OT&gt;0&lt;/OT&gt; &lt;Shots&gt;4&lt;/Shots&gt; &lt;ShotPctg&gt;-0,6000004&lt;/ShotPctg&gt; &lt;ShiftsPerGame&gt;-0,09999847&lt;/ShiftsPerGame&gt; &lt;FOWinPctg&gt;0,09999847&lt;/FOWinPctg&gt; &lt;/Player&gt; [...] &lt;/StatsDiff&gt; </code></pre> <p>I have spared to show the implementation for the CalculateDifferential() method, it is rather cryptic but it is fast and efficient. This way I could obtain the results wanted without using any other reference but the strict minimum, without having to use XSL...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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