How can i compare two XmlNodes regarding the content?

Posted on 2008-11-04
Medium Priority
Last Modified: 2012-05-05
I will compare two XmlNodes in C# regarding the content. Let´s say i have two XmlNode´s:

            XmlDocument docA = new XmlDocument();
            docA.LoadXml("<Result name="power" value="3">");
            XmlNode nodeA = docA.SelectSingleNode("/*");
            XmlDocument docB = new XmlDocument();
            docB.LoadXml("<Result value="3" name="power">");
            XmlNode nodeB = docB.SelectSingleNode("/*");

Is there any function to compare theses nodes to get the result that they are the same (regarding content)?
The order of the attribute should have no matter.
Question by:clevertrevor74
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 400 total points
ID: 22877357
attribute orders in nodes does not matter - if you are comparing the attributes values and not the string value (string/outer xml will always be different if the order of attribute are different)

use the following code snippet for comparison between nodes
XmlDocument docA = new XmlDocument ();
docA.LoadXml ("<Result name=\"power\" value=\"3\">");
XmlNode nodeA = docA.SelectSingleNode ("/*");
XmlDocument docB = new XmlDocument ();
docB.LoadXml ("<Result value=\"3\" name=\"power\">");
XmlNode nodeB = docB.SelectSingleNode ("/*");
if (nodeA.Attributes.GetNamedItem("name").Value.ToString() == nodeB.Attributes.GetNamedItem("name").Value.ToString() && nodeA.Attributes.GetNamedItem("value").Value.ToString() == nodeB.Attributes.GetNamedItem("value").Value.ToString())
	Console.WriteLine("same nodes");
	Console.WriteLine("different nodes");

Open in new window


Accepted Solution

clevertrevor74 earned 0 total points
ID: 22883989
Thank you for answering. It´s not the exact solution i want because i do not know the names of the attributes.

In the meantime i found a solution by myself. There is the library XML diff patch. See the code snippet. You can also find the differences with this classes.
XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreWhitespace);
XmlDocument docA = new XmlDocument();
docA.LoadXml ("<Result name=\"power\" value=\"3\">");
XmlDocument docB = new XmlDocument();
docB.LoadXml ("<Result value=\"3\" name=\"power\">");
bool isTheSameContent = diff.Compare(docA, docB); // It is the same (content) !

Open in new window


Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question