——此文章摘自《C#高级编程(第
版)》定价
元 特价
元 购买
图
如果要把这些书的成本相加该怎么办?XPathNavigator为此包含了Evaluate方法Evaluate有个重载方法第一个包含一个字符串该字符串就是XPath函数调用第二个Evaluate重载方法的参数是XPathExpression对象第三个Evaluate重载方法的参数是XPathExpression 和 XPathNodeIterator如果对示例进行下述修改(这个版本的代码在XPathXSLSample中)
private void button_Click(object sender SystemEventArgs e)
{
//modify to match your path structure
XPathDocument doc = new XPathDocument(\\\\\\booksxpathXML);
//create the XPath navigator
XPathNavigator nav = docCreateNavigator();
//create the XPathNodeIterator of book nodes
// that have genre attribute value of novel
XPathNodeIterator iter = navSelect(/bookstore/book[@genre=novel]);
while(iterMoveNext())
{
LoadBook(iterCurrentClone());
}
//add a break line and calculate the sum
listBoxItemsAdd(========================);
listBoxItemsAdd(Total Cost =
+ navEvaluate(sum(/bookstore/book[@genre=novel]/price)));
}
这次可以看到列表框中书籍的总成本如图所示
图
[] []