2

I have this XML:

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  <book id="ISBN-NUMBER">
    <title>
      The Autobiography of Benjamin Franklin
    </title>
    <author type="major">
      <first-name>
        Benjamin
      </first-name>
      <last-name>
        Franklin
      </last-name>
    </author>
    <price>
      8.99
    </price>
  </book>
  <book id="ISBN-NUMBER">
    <title>
      The Confidence Man
    </title>
    <author type="major">
      <first-name>
        Herman
      </first-name>
      <last-name>
        Melville
      </last-name>
    </author>
    <price>
      11.99
    </price>
  </book>
  <book id="ISBN-NUMBER">
    <title>
      The Gorgias
    </title>
    <author type="major">
      <name>
        Plato
      </name>
    </author>
    <price>
      9.99
    </price>
  </book>
</bookstore>

How do i read it with XPath? I've used:

XPathDocument doc = new XPathDocument(stream);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator node = nav.Select("bookstore/book");

while (node.MoveNext())

but how do i move on from here? i guess i need a swtich statement to swtich if node is title, author, and price. if book then i need to read books id, same with author and type.

3
  • 4
    During waiting for an answer accept some answers on previous questions Commented Feb 15, 2012 at 12:31
  • It looks like you read this example - support.microsoft.com/kb/308333; it explains how to get down the the book nodes, but not how to select the child attributes - the example msdn.microsoft.com/en-us/library/… contains this info. Commented Feb 15, 2012 at 12:58
  • You have forgotten to tell us the most important: What is your task? Which exactly nodes of the XML document do you want to select? Please, edit the question and add this missing, important information. Commented Feb 15, 2012 at 13:45

1 Answer 1

2

You can use node.Select('//title') to get the title, I suppose. I'm not sure how this works in C#, but common xpaths are as follows

  • //bokstore/book selects book nodes.
  • //bookstore/book/title selects the title node
  • //bookstore/book[n]/* selects all the all the child nodes of nth node.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.