curiouswebster
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!
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I am new to xpath and really did not want to re-invent the wheel. Cheers!
ASKER
Thanks!
ASKER
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?
Open in new window