<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
需要求出:<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
1: 所有书的总价,即sum(price)
2:ISBN="0-201-63361-2"的书的价格,使用XPath可以表达: /bookstore/book[@ISBN="0-201-63361-2"]/price
简单分析:
XPathNavigator/XPathExpression 是2个很重要的类,顾名思义,XPathNavigator是用来定位的Node上下文的,定位之后,才能求出相对的Xpath值。XPathExpression是表达式
大家看看代码就清楚了:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Xml;
namespace XPathNavigatorSample
{
class Program
{
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("books.xml");
XmlNode xnode = xdoc.SelectSingleNode("/bookstore/book");
XPathNavigator navigator = xnode.CreateNavigator();
//XPathExpression query = navigator.Compile("string(sum(price))");
//Evaluate(query, navigator);
query = navigator.Compile("string(sum(//price))");
Evaluate(query, navigator);
XPathExpression query2 = navigator.Compile("string(//book[@ISBN='0-201-63361-2']/price)");
Evaluate(query2, navigator);
}
public static void Evaluate(XPathExpression expression, XPathNavigator navigator)
{
switch (expression.ReturnType)
{
case XPathResultType.Number:
Console.WriteLine(navigator.Evaluate(expression));
break;
case XPathResultType.NodeSet:
XPathNodeIterator nodes = navigator.Select(expression);
while (nodes.MoveNext())
{
Console.WriteLine(nodes.Current.ToString());
}
break;
case XPathResultType.Boolean:
if ((bool)navigator.Evaluate(expression))
Console.WriteLine("True!");
break;
case XPathResultType.String:
Console.WriteLine(navigator.Evaluate(expression));
break;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;
using System.Xml;
namespace XPathNavigatorSample
{
class Program
{
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("books.xml");
XmlNode xnode = xdoc.SelectSingleNode("/bookstore/book");
XPathNavigator navigator = xnode.CreateNavigator();
//XPathExpression query = navigator.Compile("string(sum(price))");
//Evaluate(query, navigator);
query = navigator.Compile("string(sum(//price))");
Evaluate(query, navigator);
XPathExpression query2 = navigator.Compile("string(//book[@ISBN='0-201-63361-2']/price)");
Evaluate(query2, navigator);
}
public static void Evaluate(XPathExpression expression, XPathNavigator navigator)
{
switch (expression.ReturnType)
{
case XPathResultType.Number:
Console.WriteLine(navigator.Evaluate(expression));
break;
case XPathResultType.NodeSet:
XPathNodeIterator nodes = navigator.Select(expression);
while (nodes.MoveNext())
{
Console.WriteLine(nodes.Current.ToString());
}
break;
case XPathResultType.Boolean:
if ((bool)navigator.Evaluate(expression))
Console.WriteLine("True!");
break;
case XPathResultType.String:
Console.WriteLine(navigator.Evaluate(expression));
break;
}
}
}
}
源代码:/Files/cleo/XPathNavigator.rar