How can i compare two XmlNodes regarding the content?

Posted on 2008-11-04
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
    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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now