Question

var xml= @"<?xml version='1.0'?>
                 <bookstore>
                 <book genre=""3"">
                   <title>The Gorgias3</title>
                   <author>
                     <name>Plato3</name>
                   </author>
                   <price>3</price>
                 </book>
            <book genre=""4"">
                   <title>The Gorgias4</title>
                   <author>
                     <name>Plato4</name>
                   </author>
                   <price>4</price>
                 </book>
            </bookstore>";



XPathDocument docNav = new XPathDocument(new StringReader(xml));
XPathNavigator navigator = docNav.CreateNavigator();
XPathNodeIterator NodeIter = navigator.Select("/bookstore/book/title");

foreach (XPathNavigator selectedNode in NodeIter)
{
    Console.WriteLine(selectedNode.Name);
}

How can i access straight to the first /bookstore/book/title" child and get his value.

I dont want to iterate and break the loop

the result should be "The Gorgias3"

p.s. i dont want to modify the xpath expression with the first filter.

I want to Know how i can do it with c#.

Was it helpful?

Solution

Use:

var node = navigator.SelectSingleNode("/bookstore/book/title");

Console.WriteLine(node);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top