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<int, XNode> nodeDictionary = new Dictionary<int, XNode>();
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:
<?xml version="1.0" encoding="utf-8" ?>
<doc>
<node att="A">Blah</node>
<node att="A">Blah</node>
<node att="B">
<inner>Innertext</inner>
</node>
<node>Blah</node>
<node att="B">
<inner>Different</inner>
</node>
</doc>
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.
What about this approach:
For all <w:pPr>
nodes in the document (I suppose there is not more than one per <w:p>
), concatenate all relevant data (element names, attributes, values) into a string:
// string format is really irrelevant, so this is just a bogus example
'!w:keep-with-next@value="true"!w:spacing@w:before="10"@w:after="120"'
Do so on alphabetical order, to account for varying document order.
Build a collection using these strings as the key and the reference to the respective <w:p>
node as the value.
In the process of doing this, when you hit the point that a given key already exists in the collection, you found a paragraph with the same properties. Work with a list of nodes as the collection value, if you want to keep collecting.
I can't say how well this would perform, but I guess it is not too hard to implement and find out.
It is very challenging even to define correctly the problem of
"When two xml documents are equal?"
There are many reasons for this:
Therefore it seems naive and unrealistic to try to produce a correct implementation of a function for equality comparison of two XML documents.
My recommendation is to use the deep-equal() function with a compliant XPath 2.0 engine.
not a direct answer to your question, but closely related to what you are trying to acheive: have a look at XmlDiff (.net XML power tools)
Here is a hash function I have knocked up that attempts to solve a part of your problem. Note that I have very little experience writing hash functions, and have included it mainly to get feedback from people as to it's effectiveness in solving this particular problem. I would not recommend it's use in production.
static int HashXElement(XElement elem)
{
int hash = 23;
foreach (XAttribute attrib in elem.Attributes())
{
int attribHash = 23;
attribHash = attribHash * 37 + attrib.Name.GetHashCode();
attribHash = attribHash * 37 + attrib.Value.GetHashCode();
hash = hash ^ attribHash;
}
foreach(XElement subElem in elem.Descendants())
{
hash = hash * 37 + XmlHash(subElem);
}
hash = hash * 37 + elem.Value.GetHashCode();
return hash;
}
The ideas was to make the ordering of subnodes significant, but the ordering of attributes not significant.