从XML获取Node值

时间:2008-11-24 03:16:26

标签: .net xml xpath

如何使用XPathNavigator.Evaluate(或XPathNavigator中的其他方法)获取以下xml输入的ISBN值?

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

2 个答案:

答案 0 :(得分:3)

XPathIterator xit = XPathNavigator1.Select("/bookstore/book/@bk:ISBN");
xit.MoveNext();
String value = xit.Current.Value;

您想要的值位于xit.Current.Value

顺便说一下,我建议您查看xPath上的this great article

答案 1 :(得分:3)

amdfan给出的答案几乎是正确的。这是正确的语法:

XPathIterator xit = XPathNavigator1.Select("/bookstore/book/@bk:ISBN");
xit.MoveNext();
String value = xit.Current.Value;

我在VS 2008中对此进行了测试。

相关问题