Relative XPath node selection with C# XmlDocument
Posted
by lox
on Stack Overflow
See other posts from Stack Overflow
or by lox
Published on 2009-06-05T13:55:38Z
Indexed on
2010/03/23
13:23 UTC
Read the original article
Hit count: 723
Imagine the following XML document:
<root>
<person_data>
<person>
<name>John</name>
<age>35</age>
</person>
<person>
<name>Jim</name>
<age>50</age>
</person>
</person_data>
<locations>
<location>
<name>John</name>
<country>USA</country>
</location>
<location>
<name>Jim</name>
<country>Japan</country>
</location>
</locations>
</root>
I then select the person node for Jim:
XmlNode personNode = doc.SelectSingleNode("//person[name = 'Jim']");
And now from this node with a single XPath select I would like to retrieve Jim's location node. Something like:
XmlNode locationNode = personNode.SelectSingleNode("//location[name = {reference to personNode}/name]");
Since I am selecting based on the personNode it would be handy if I could reference it in the select. Is this possible?.. is the connection there?
Sure I could put in a few extra lines of code and put the name into a variable and use this in the XPath string but that is not what I am asking.
© Stack Overflow or respective owner