
图 23-7
如果要把这些书的成本相加,该怎么办?XPathNavigator为此包含了Evaluate方法。Evaluate有3个重载方法,第一个包含一个字符串,该字符串就是XPath函数调用。第二个Evaluate重载方法的参数是XPathExpression对象,第三个Evaluate重载方法的参数是XPathExpression 和 XPathNodeIterator。如果对示例进行下述修改(这个版本的代码在XPathXSLSample2中):
private void button1_Click(object sender, System.EventArgs e)
{
//modify to match your path structure
XPathDocument doc = new XPathDocument("..\\..\\..\\booksxpath.XML");
//create the XPath navigator
XPathNavigator nav = doc.CreateNavigator();
//create the XPathNodeIterator of book nodes
// that have genre attribute value of novel
XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']");
while(iter.MoveNext())
{
LoadBook(iter.Current.Clone());
}
//add a break line and calculate the sum
listBox1.Items.Add("========================");
listBox1.Items.Add("Total Cost = "
+ nav.Evaluate("sum(/bookstore/book[@genre='novel']/price)"));
}
这次,可以看到列表框中书籍的总成本,如图23-8所示。

图 23-8

一沙一世界 一花一天堂 掌中握無……

网络编程技术、多媒体技术、PC应用技术
