Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

How do I get a certain XmlNode?

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

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I am new to xpath and really did not want to re-invent the wheel. Cheers!
Thanks!