darma teja

darma teja

  • NA
  • 496
  • 338.7k

How to put xpath in xdocument

Apr 5 2013 6:11 AM
Hi All,

my xml file is like this:

<bookstore>
  <book category="COOKING">
    <title  id="1">Everyday Italian</title>
  </book>
  <book category="CHILDREN">
    <title id="2">Harry Potter</title>
  </book>
  <book category="WEB">
    <title id="3">Learning XML</title>
  </book>
</bookstore>

I would like to count how many titles in my xml file. for this I used code like this: and It works fine.
 
var count = XDocument.Load(path).XPathSelectElements("//title").Count();

It means that it goes to each tag and counts. No I do not want o give more burden on my tool. just use the xpath query to get last title id value.
I tried like this. But it is giving me exception.

var count = XDocument.Load(path).XPathSelectElement("
title[last()]/@id").Value;

Thanks,

Darma




Answers (1)