要理解这些类的用法,最好是查看一下迭代books.xml文档的代码,确定导航是如何工作的。为了使用这些示例,首先需要添加对System.Xml.Xsl 和 System.Xml.XPath命名空间的引用,如下所示:
using System.Xml.XPath;
using System.Xml.Xsl;
这个示例使用了文件booksxpath.xml,它类似于前面使用的books.xml,但booksxpath.xml添加了两本书。下面是窗体代码,这段代码在XPathXSLSample1文件夹中:
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);
}
}
private void LoadBook(XPathNavigator lstNav)
{
//We are passed an XPathNavigator of a particular book node
//we will select all of the descendents and
//load the list box with the names and values
XPathNodeIterator iterBook=lstNav.SelectDescendants
(XPathNodeType.Element, false);
while(iterBook.MoveNext())
listBox1.Items.Add(iterBook.Current.Name + ": "
+ iterBook.Current.Value);
}
XPathNavigator nav = doc.CreateNavigator();
要把数据加载到列表框中,使用XPathNodeIterator.Current属性,根据XPathNodeIterator指向的节点,创建一个新的XPathNavigator对象。在本例中,为文档中的一个book节点创建一个XPathNavigator。
LoadBook()方法提取这个XPathNavigator,调用Select方法的另一个重载方法SelectDescendants创建另一个XPathNavigator,这样,XPathNodeIterator就包含了给LoadBook方法发送的book节点的所有子节点。

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

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

| 我爱研发网 | 中电华信 | 阿里西西 | JAVA爱好者 | 北京英才网 | 全球大学查询网 |
| 中国人的网站导航 | 中国电脑论坛 | 信息产业部 | 新浪科技 | 搜狐IT | 信息产业部电子教育与考试中心 |
| IT世界网 | 软件项目交易网 | 中国软件交易网 | 国信培训网 | 亚远景科技 | ....[更多] |