开发者

XML Reading - node access

开发者 https://www.devze.com 2023-02-23 22:09 出处:网络
<node label=\"Chemist Name\"> <node label=\"John,Smith\" searchId=\"1122\" /> </node>
<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消