XPath和XSL转换----用XPathNavigator定位
如何使用 XPathNavigator 浏览 XML
此示例阐释如何使用从 XPathDocument 创建的 XPathNavigator 浏览 XML 文档。还说明如何使用 XPathNodeIterator。XPathNavigator 类提供光标样式的模型来浏览 XML 文档。该主题还介绍了 XML 路径语言 (XPath) 表达式,这在如何使用 XPath 表达式查询 XML中有更多的详细信息。
此示例加载一个 XPathDocument(包含 books.xml 中的 XML 数据),创建一个 XPathNavigator 作为查看这些数据的视图,然后通过在文档中移动来显示该 XML。XPathNavigator 类提供对数据的 XPath API,如 XPathDocument。下列示例代码显示创建这些类的过程。
XPathDocument myXPathDocument = new XPathDocument(args); ... //Create an XPathNavigator XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator(); |
||
C# | VB |
XPathNavigator 使您可以在 XML 文档中的属性节点和命名空间节点中移动。
XPathNavigator 的方法和属性相对于当前节点进行操作:
- MoveToFirstChild 方法移到当前节点的第一个子节点。
- MoveToParent 方法移到当前节点的父节点。当定位在某个属性上时,必须使用 MoveToParent 方法移到拥有该属性节点的元素上。这是在使用 MoveToFirstAttribute 和 MoveToNextAttribute 浏览完该元素的属性后如何回到该元素的方法。
- HasChildren 属性指示当前节点是否有子节点。
- HasAttributes 属性指示当前节点是否有属性节点。
- MoveToRoot 方法将导航器定位到包含整个节点树的文档节点上。
这些方法使您得以以递归方式浏览 XML 文档,如下列代码所示。
myXPathNavigator.MoveToRoot(); // Initialize the myXPathNavigator to start at the root DisplayTree(myXPathNavigator); // Display all the nodes ... // Walks the XPathNavigator tree recursively public void DisplayTree (XPathNavigator myXPathNavigator) { if (myXPathNavigator.HasChildren) { myXPathNavigator.MoveToFirstChild(); Format (myXPathNavigator); DisplayTree (myXPathNavigator); myXPathNavigator.MoveToParent(); } while (myXPathNavigator.MoveToNext()) { Format (myXPathNavigator); DisplayTree (myXPathNavigator); } } // Format the output private void Format (XPathNavigator myXPathNavigator) { if (!myXPathNavigator.HasChildren) { if (myXPathNavigator.NodeType == XPathNodeType.Text) Console.WriteLine(myXPathNavigator.Value ); else if (myXPathNavigator.Name != String.Empty) Console.WriteLine("<" + myXPathNavigator.Name + ">"); else Console.WriteLine(); } else { Console.WriteLine("<" + myXPathNavigator.Name + ">"); // Show the attributes if there are any if (myXPathNavigator.HasAttributes) { Console.WriteLine("Attributes of <" + myXPathNavigator.Name + ">"); while (myXPathNavigator.MoveToNextAttribute()) Console.Write("<" + myXPathNavigator.Name + "> " + myXPathNavigator.Value + " "); // Return to the 'Parent' node of the attributes myXPathNavigator.MoveToParent(); } } } |
||
C# | VB |
如果您知道该 XML 文档的结构,则可以通过在文档中显式地从一个节点移动到另一个节点来浏览文档。下列代码在示例文件 books.xml 中查找第一个书籍节点的价格。
// Find the price of the first book. Start at the root node Console.WriteLine (); Console.WriteLine ("Find the price of the first book by navigating nodes ..."); myXPathNavigator.MoveToRoot(); DisplayNode (true, myXPathNavigator); // root node DisplayNode (myXPathNavigator.MoveToFirstChild(), myXPathNavigator); // ?xml version='1.0'? node DisplayNode (myXPathNavigator.MoveToNext(), myXPathNavigator); //!-- This file ... node DisplayNode (myXPathNavigator.MoveToNext(), myXPathNavigator); // bookstore element DisplayNode (myXPathNavigator.MoveToFirstChild(), myXPathNavigator); // book element DisplayNode (myXPathNavigator.MoveToFirstChild(), myXPathNavigator); // title element DisplayNode (myXPathNavigator.MoveToNext(), myXPathNavigator);// author element DisplayNode (myXPathNavigator.MoveToNext(), myXPathNavigator); // price Element DisplayNode (myXPathNavigator.MoveToFirstChild(), myXPathNavigator); // value of price element ... private void DisplayNode(Boolean success, XPathNavigator myXPathNavigator) { if (success && (myXPathNavigator.NodeType == XPathNodeType.Text)) Console.WriteLine(myXPathNavigator.Value ); else if (success && (myXPathNavigator.Name != String.Empty)) Console.WriteLine("<" + myXPathNavigator.Name + ">"); else Console.WriteLine(); } |
||
C# | VB |
下列代码说明了 XPathNavigator 最强大的功能之一——即可使用 Select 方法从 XPath 表达式创建一组选定的节点。XPath 是一种提供对 XML 文档中的数据进行筛选和寻址的语言,并且是检索节点集的另一种方法。XPathNavigator 可应用 XPath 表达式,并生成与选定表达式相对应的节点集。然后,您可以单独浏览该节点集,因为这些节点被返回到 XPathNodeIterator,其 MoveNext 方法允许对选定节点树进行只读浏览。下列代码示例选择并显示示例文件 books.xml 中每个书籍节点的标题子节点。
XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select("descendant::book/title"); while(myXPathNodeIterator.MoveNext()) { Console.WriteLine("<" + myXPathNodeIterator.Current.Name + ">" + myXPathNodeIterator.Current.Value); }; |
||
C# | VB |
摘要
- XPathNavigator 类提供光标样式的模型以在内存中浏览 XML 文档。
- XPathNavigator 类提供对数据的 XPath API,如 XPathDocument。
- XPathNavigator 使您可以在 XML 文档中的属性节点和命名空间节点中移动。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步