Relative XPath node selection with C# XmlDocument
- by lox
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.