I want to determine whether two different child nodes within an XML document are equal or not. Two nodes should be considered equal if they have the same set of attributes a
I'd recommend against rolling your own hash creation function and instead rely on the in-built XNodeEqualityComparer
's GetHashCode
method. This guarantees to take account of attributes and descendant nodes when creating the result and could save you some time too.
Your code would look like the following:
XNodeEqualityComparer comparer = new XNodeEqualityComparer();
XDocument doc = XDocument.Load("XmlFile1.xml");
Dictionary nodeDictionary = new Dictionary();
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);
}
}
My XmlFile1.xml is:
Blah
Blah
Innertext
Blah
Different
nodeDictionary
will end up containing a unique collection of Nodes and their hashes. Duplicates are detected by using the Dictionary
's ContainsKey
method, passing in the hash of the node, which we generate using the XNodeEqualityComparer
's GetHashCode
method.
I think this should be fast enough for your needs.