XML Reading - node access
<node label="Chemist Name">
<node label="John,Smith" searchId="1122" />
</node>
Hi,
if i have the above as part of my xml structure, and i want to find a tag where the parent has a label of "Chemist Name" and its inner tag has a label of John,Smith so i can then get the searchid - what would be the best way of doing it?
is there a way where i can, rather than unefficiently looping through every xml value in my document just directly say
"get me开发者_如何学C the node where its parent is chemist name and its child has a label of john smith"
thanks
Use XPath with the following query:
//node[@label = "Chemist Name"]/node[@label = "John,Smith"]
You can use it like this in C#:
var doc = new XmlDocument();
doc.LoadXml(xml);
var node = doc.SelectSingleNode(
@"//node[@label = ""Chemist Name""]/node[@label = ""John,Smith""]");
Where xml
is a string containing the XML data. If you want to load the XML directly from disc, use XmlDocument.Load()
instead.
xpath is the way to go with this. you should read more on the XPathNavigator, and xpath. If you're stuck, post back and we can help out.
精彩评论