Efficient algorithm for comparing XML nodes

前端 未结 5 1984
灰色年华
灰色年华 2020-12-29 10:40

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

5条回答
  •  天涯浪人
    2020-12-29 11:16

    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.

提交回复
热议问题