Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:15:07+00:00 2026-05-11T00:15:07+00:00

I want to determine whether two different child nodes within an XML document are

  • 0

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 and child notes and all child notes are equal, too (i.e. the whole sub tree should be equal).

The input document might be very large (up to 60MB, more than a 100000 nodes to compare) and performance is an issue.

What would be an efficient way to check for the equality of two nodes?

Example:

<w:p>   <w:pPr>     <w:spacing w:after='120'/>   </w:pPr>   <w:r>     <w:t>Hello</w:t>   </w:r> </w:p> <w:p>   <w:pPr>     <w:spacing w:after='240'/>   </w:pPr>   <w:r>     <w:t>World</w:t>   </w:r> </w:p> 

This XML snippet describes paragraphs in an OpenXML document. The algorithm would be used to determine whether a document contains a paragraph (w:p node) with the same properties (w:pPr node) as another paragraph earlier in the document.

One idea I have would be to store the nodes’ outer XML in a hash set (Normally I would have to get a canonical string representation first where attributes and child notes are sorted always in the same way, but I can expect my nodes already to be in such a form).

Another idea would be to create an XmlNode object for each node and write a comparer which compares all attributes and child nodes.

My environment is C# (.Net 2.0); any feedback and further ideas are very welcome. Maybe somebody even has already a good solution?

EDIT: Microsoft’s XmlDiff API can actually do that but I was wondering whether there would be a more lightweight approach. XmlDiff seems to always produce a diffgram and to always produce a canonical node representation first, both things which I don’t need.

EDIT2: I finally implemented my own XmlNodeEqualityComparer based on the suggestion made here. Thanks a lot!!!!

Thanks, divo

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:15:08+00:00Added an answer on May 11, 2026 at 12:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 110k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just one crash dump, although the log file may contain… May 11, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer Loop through it once and build an associative array, or… May 11, 2026 at 9:35 pm
  • Editorial Team
    Editorial Team added an answer you can just do: element.removeAttribute("style") May 11, 2026 at 9:35 pm

Related Questions

This could be considered a continuation of this earlier SO question . Ideally, I'd
In my matlab program, I want to determine whether a variable or output of
I am writing some code to determine whether a network domain is registered. For
I have written a message board as my first ASP.NET project. It seems to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.