2008-11-24 216 views
0

如何使用XPathNavigator.Evaluate(或XPathNavigator中的其他方法)獲取以下xml輸入的ISBN值?從XML獲取節點值

<?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> 

回答

3

amdfan給出的答案几乎是正確的。下面是正確的語法:

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

我測試了在VS 2008

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

你想要的值是xit.Current.Value

順便說一句,我建議你XPath的檢查this great article

+0

的語法幾乎是correct--它應該是XPathNavigator1.Select( 「/書店/書/ @ BK:ISBN」); – Graviton 2008-11-24 04:41:45