Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd recommend against rolling your own hash creation function and instead rely on the in-built <code>XNodeEqualityComparer</code>'s <code>GetHashCode</code> method. This guarantees to take account of attributes and descendant nodes when creating the result and could save you some time too.</p> <p>Your code would look like the following:</p> <pre><code>XNodeEqualityComparer comparer = new XNodeEqualityComparer(); XDocument doc = XDocument.Load("XmlFile1.xml"); Dictionary&lt;int, XNode&gt; nodeDictionary = new Dictionary&lt;int, XNode&gt;(); foreach (XNode node in doc.Elements("doc").Elements("node")) { int hash = comparer.GetHashCode(node); if (nodeDictionary.ContainsKey(hash)) { // A duplicate has been found. Execute your logic here // ... } else { nodeDictionary.Add(hash, node); } } </code></pre> <p>My XmlFile1.xml is:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;doc&gt; &lt;node att="A"&gt;Blah&lt;/node&gt; &lt;node att="A"&gt;Blah&lt;/node&gt; &lt;node att="B"&gt; &lt;inner&gt;Innertext&lt;/inner&gt; &lt;/node&gt; &lt;node&gt;Blah&lt;/node&gt; &lt;node att="B"&gt; &lt;inner&gt;Different&lt;/inner&gt; &lt;/node&gt; &lt;/doc&gt; </code></pre> <p><code>nodeDictionary</code> will end up containing a unique collection of Nodes and their hashes. Duplicates are detected by using the <code>Dictionary</code>'s <code>ContainsKey</code> method, passing in the hash of the node, which we generate using the <code>XNodeEqualityComparer</code>'s <code>GetHashCode</code> method.</p> <p>I think this should be fast enough for your needs.</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