We help IT Professionals succeed at work.

How do I get a certain XmlNode?

curiouswebster
curiouswebster asked
on
I thought I could iterate through the CholdNodes belonging to the root node returned from SelectSingleNode() and find my node. I can seee it in the InnerXML so I know it's there. But I do not want to parse the XML like it's text to get the value out.

How can I iterate through it?  The attached recursive function I wrote, I thought, would solve the problem. But it does not.

Does anyone know how I can get the XmlNode node for a given XmlTag?

Thanks!
private XmlNode GetNode(XmlNode node, string xmlTag)
        {
            if (node != null && node.HasChildNodes)
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.Name == xmlTag)
                    {
                        return childNode;
                    }
                    if (childNode.HasChildNodes)
                    {
                        return GetNode(childNode, xmlTag);
                    }
                }
            }
            return null;
        }

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015
Commented:
Your recursion is flawed. If your code ever reaches the "return null;" line, which is highly likely based on your given conditions, then your code is going to admit defeat before it has even searched the whole document. Try this simple modification:

private static XmlNode GetNode(XmlNode node, string xmlTag)
{
    if (node != null && node.HasChildNodes)
    {
        foreach (XmlNode childNode in node.ChildNodes)
        {
            if (childNode.Name == xmlTag)
            {
                return childNode;
            }
            if (childNode.HasChildNodes)
            {
                XmlNode result = GetNode(childNode, xmlTag);

                if (result != null)
                {
                    return result;
                }
            }
        }
    }

    return null;
}

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
Thanks. I see the difference that change makes. But it still does not extract an XmlNode.

I think my problem is that the Name property does not match the name="myName".

For example, the name might be "message" since that's the tag we defined, thusly.

<messages>
<message name="myName">
<![CDATA[My Message]]>
</message>
</messages>

The tag is "myName" and the content I want to get out is "My Message".

I am confused because the Name of the node is "message" and I do not know how to get the value associated with name=.

Can I get that by modifying my function? If so, how?
private static XmlNode GetNode(XmlNode node, string xmlTag)
        {
            if (node != null)
            {
                if (node.Name == xmlTag)
                {
                    return node;
                }
                if (node.HasChildNodes)
                {
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.Name == xmlTag)
                        {
                            return childNode;
                        }
                        if (childNode.HasChildNodes)
                        {
                            XmlNode result = GetNode(childNode, xmlTag);

                            if (result != null)
                            {
                                return result;
                            }
                        }
                    }
                }
            }

            return null;
        }

Open in new window

Senior Software Engineer
Top Expert 2009
Commented:
No need for custom code.You are better off using xpath:
http://www.w3schools.com/xpath/xpath_syntax.asp

In your case: (aasume xNode is the node to search)
var result = xNode.SelectSingleNode("//message[@name='myName']");
//use result
curiouswebsterSoftware Engineer

Author

Commented:
Thanks. I am new to xpath and really did not want to re-invent the wheel. Cheers!
curiouswebsterSoftware Engineer

Author

Commented:
Thanks!