ricki
ricki

Reputation: 159

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 the node where its parent is chemist name and its child has a label of john smith"

thanks

Upvotes: 0

Views: 300

Answers (2)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

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.

Upvotes: 3

jasper
jasper

Reputation: 3474

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.

Upvotes: 0

Related Questions