I’m having a problem with reading and processing a xml file, which I cannot solve right now.
The xml has the following structure:
<root>
<test id="1">
<a></a>
<b></b>
<c></c>
</test>
<test id="2">
<a></a>
<b></b>
<c></c>
</test>
<test id="3">
<a></a>
<b></b>
<c></c>
</test>
</root>
XmlDocument Doc; int currentid=1;
XmlNode currentlyselectedtestnode =
Doc.SelectNodes("//test[@id = '" +
currentid.ToString() + "']");
string a = currentlyselectedtestnode.SelectSingleNode("//a");
string b = currentlyselectedtestnode.SelectSingleNode("//b");
string c = currentlyselectedtestnode.SelectSingleNode("//c");
Unfortunately, “currentlyselectedtestnode.SelectSingleNode(“//a”)” will read out all “a”-nodes and not only the one that belongs to test-node with id 1. Why ?!
Somehow currentlyselectedtestnode.SelectSingleNode(“//a”); works just as if I wrote Doc.SelectSingleNode(“//a”);
How come ?! How can I make it read the children of the specific test-node only ?ectedtestnode.SelectSingleNode(“//c”);
When using
//ain XPath, you are selecting allanodes in the document.If you want the direct child, you need to use
currentlyselectedtestnode.SelectSingleNode("a").See XPath Syntax on w3schools:
You can select all
anodes that are under the current node by using.//a. This will select all childanodes of the current node, regardless of how deeply nested they are.