简体   繁体   English

从XML获取Node值

[英]Get the Node value from XML

How do I use XPathNavigator.Evaluate ( or other methods in XPathNavigator) to obtain the ISBN value for the following xml input? 如何使用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>
XPathIterator xit = XPathNavigator1.Select("/bookstore/book/@bk:ISBN");
xit.MoveNext();
String value = xit.Current.Value;

The value you want is in xit.Current.Value 所需的值在xit.Current.Value

By the way, I recommend you check out this great article on xPath. 顺便说一句,我建议您查看有关xPath的精彩文章

The answer given by amdfan is almost correct. amdfan给出的答案几乎是正确的。 Here's the correct syntax: 这是正确的语法:

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

I tested this in VS 2008. 我在VS 2008中对此进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM