Handling duplicate nodes in XML

Posted by JYelton on Stack Overflow See other posts from Stack Overflow or by JYelton
Published on 2010-05-04T19:14:05Z Indexed on 2010/05/04 19:18 UTC
Read the original article Hit count: 301

Filed under:
|

Scenario:

I am parsing values from an XML file using C# and have the following method:

    private static string GetXMLNodeValue(XmlNode basenode, string strNodePath)
    {
        if (basenode.SelectSingleNode(strNodePath) != null)
            return (basenode.SelectSingleNode(strNodePath).InnerText);
        else
            return String.Empty;
    }

To get a particular value from the XML file, I generally pass the root node and a path like "parentnode/item"

I recently ran into an issue where two nodes at the same document level share the same name.

Question:

What is the best way to get the values for duplicate-named nodes distinctly? My thought was to load all values matching the node name into an array and then using the array index to refer to them. I'm not sure how to implement that, though. (I'm not well-versed in XML navigation.)

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml