Xpath

XML Path 语言 (Xpath) 表达式使用路径表示法(像在 URL 中使用的一样)来为 XML 文档的各部分寻址。 表达式计算为生成节点集、布尔值、数字或字符串类型的对象。 例如,表达式 book/author 将返回包含在 <book> 元素中的 <author> 元素的节点集(如果这种元素已在源 XML 文档中声明的话)。 此外,XPath 表达式还可以包含谓词(筛选表达式)或函数调用。 例如,表达式 book[@type="Fiction"] 引用其 type 属性已设置为 "Fiction" 的 <book> 元素。

XPath 表达式是使用下表中所示的运算符和特殊字符构造的。

/

子运算符;选择左侧集合的直接子级。 

此路径运算符出现在模式开头时,表示应从根节点选择该子级。

//

递归下降;在任意深度搜索指定元素。 

此路径运算符出现在模式开头时,表示应从根节点递归下降。

.

指示当前上下文。

..

当前上下文节点的父级。

*

通配符;选择所有元素,与元素名无关。

@

属性;属性名的前缀。

@*

属性通配符;选择所有属性,与名称无关。

:

命名空间分隔符;将命名空间前缀与元素名或属性名分隔。

( )

为运算分组,明确设置优先级。

[ ]

应用筛选模式。

[ ]

下标运算符;用于在集合中编制索引。

+

执行加法。

-

执行减法。

div

根据 IEEE 754 执行浮点除法。

*

执行乘法。

mod

从截断除法返回余数。

此表不包括布尔运算符和集运算符,这两个运算符在布尔、比较和集表达式集运算中列出。

优先级顺序(从最高优先级到最低优先级)的定义如下表所示。

 

优先级

字符

用途

1

( )

分组

2

[ ]

筛选器

3

/ //

路径运算

分组运算符 () 仅适用于顶级路径表达式。 例如,(//author/degree | //author/name) 是有效的分组运算,但 //author/(degree | name) 不是。

筛选模式运算符 ([]) 的优先级高于路径运算符(/ 和 //)。 例如,表达式 //comment()[3] 选择相对于文档中任意位置备注的父级索引等于 3 的所有备注。 此表达式与表达式 (//comment())[3] 不同,后者选择相对于父级的所有备注集中的第三个备注。 前一个表达式可以返回多个备注,后一个表达式只能返回一个备注。

更多请查询:http://msdn.microsoft.com/zh-cn/library/ms256471.aspx

 

XMLFile.xml:

<?xml version="1.0" encoding="utf-8" ?>
<bookstore specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>
        Selected Short Stories of
        <first-name>Mary</first-name>
        <last-name>Bob</last-name>
      </publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
  <magazine style="glossy" frequency="monthly">
    <price>2.50</price>
    <subscription price="24" per="year"/>
  </magazine>
  <book style="novel" id="myfave">
    <author>
      <first-name>Toni</first-name>
      <last-name>Bob</last-name>
      <degree from="Trenton U">B.A.</degree>
      <degree from="Harvard">Ph.D.</degree>
      <award>Pulitzer</award>
      <publication>Still in Trenton</publication>
      <publication>Trenton Forever</publication>
    </author>
    <price intl="Canada" exchange="0.7">6.50</price>
    <excerpt>
      <p>It was a dark and stormy night.</p>
      <p>
        But then all nights in Trenton seem dark and
        stormy to someone who has gone through what
        <emph>I</emph> have.
      </p>
      <definition-list>
        <term>Trenton</term>
        <definition>misery</definition>
      </definition-list>
    </excerpt>
  </book>
  <my:book xmlns:my="uri:mynamespace" style="leather" price="29.50">
    <my:title>Who's Who in Trenton</my:title>
    <my:author>Robert Bob</my:author>
  </my:book>
</bookstore>
xml Code

示例:

using System;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("XMLFile.xml");
            XmlNamespaceManager nsp = new XmlNamespaceManager(xml.NameTable);
            nsp.AddNamespace("my", "my");

            // 根节点
            XmlNode rootNode = xml.DocumentElement;
            XmlNode rootNode2 = xml.SelectNodes("bookstore")[0];

            // 当前上下文节点的<author>元素
            XmlNodeList nodeList = rootNode.SelectNodes("./author");    // 相当于rootNode.SelectNodes("author");
            ShowData(nodeList); // nodeList.Count=0
            // 此文档根处的所有<author>元素
            nodeList = rootNode.SelectNodes("/author");
            ShowData(nodeList); // nodeList.Count=0
            // 此文档根处的所有<bookstore>元素
            nodeList = rootNode.SelectNodes("/bookstore");
            ShowData(nodeList); // nodeList.Count=1

            // 此文档根处下的所有<author>元素(从根节点递归下降,查找<author>元素)
            nodeList = rootNode.SelectNodes("//author");
            ShowData(nodeList); // nodeList.Count=3
            nodeList = rootNode.SelectSingleNode("magazine").SelectNodes("//author");
            ShowData(nodeList); // nodeList.Count=3
            // 当前上下文<magazine>节点的所有<author>元素(从<magazine>节点递归下降,查找<author>元素)
            nodeList = rootNode.SelectSingleNode("magazine").SelectNodes(".//author");
            ShowData(nodeList); // nodeList.Count=0

            // 当前上下文节点中的<bookstore>元素内的所有<book>元素
            nodeList = rootNode.SelectNodes("bookstore/book");
            ShowData(nodeList); // nodeList.Count=0
            nodeList = xml.SelectNodes("bookstore/book");
            ShowData(nodeList); // nodeList.Count=3
            // 当前上下文节点中的<bookstore>元素中一级或多级深度的所有<author>元素(任意后代)。 
            // 注意,此表达式与以下模式 bookstore/*/author 不同,作为<bookstore>元素的孙代的所有<author> 元素
            nodeList = rootNode.SelectNodes("bookstore//author");
            ShowData(nodeList); // nodeList.Count=0
            nodeList = xml.SelectNodes("bookstore//author");
            ShowData(nodeList); // nodeList.Count=3

            // 当前元素上下文的 style 属性。
            nodeList = rootNode.SelectNodes("@style");
            ShowData(nodeList); // nodeList.Count=0
            // 当前上下文中所有<book> 元素上的 style 属性
            nodeList = rootNode.SelectNodes("book/@style");
            ShowData(nodeList); // nodeList.Count=3
            // 当前上下文节点的所有子级元素上的 style 属性。
            nodeList = rootNode.SelectNodes("*/@style");
            ShowData(nodeList); // nodeList.Count=5
            // 当前上下文节点的所有属性
            nodeList = rootNode.SelectNodes("@*");
            ShowData(nodeList); // nodeList.Count=1

            // 当前上下文的具有 style 属性的 <book> 所有元素。
            nodeList = rootNode.SelectNodes("book[@style]");
            ShowData(nodeList); // nodeList.Count=3

            // 当前上下文节点中的第一个 <book> 元素。
            nodeList = rootNode.SelectNodes("book[1]");
            ShowData(nodeList); // nodeList.Count=1
            // 当前上下文节点中的具有<author>子级的第三个 <book> 元素。
            nodeList = rootNode.SelectNodes("book[author][3]");
            ShowData(nodeList); // nodeList.Count=1

            // 当前上下文节点中的每个 <book> 元素内的最后一个 <author> 元素。
            nodeList = rootNode.SelectNodes("book/author[last()]");
            ShowData(nodeList); // nodeList.Count=3
            // 当前上下文节点中的<book>元素内<author>元素的整个集中的最后一个<author> 元素。
            nodeList = rootNode.SelectNodes("(book/author)[last()]");
            ShowData(nodeList); // nodeList.Count=1

            Console.ReadKey();
        }

        /// <summary>
        /// 显示数据
        /// </summary>
        /// <param name="nodeList"></param>
        private static void ShowData(XmlNodeList nodeList)
        {
            Console.WriteLine("nodeList.Count=" + nodeList.Count);
            foreach (XmlNode item in nodeList)
            {
                Console.WriteLine(item.InnerXml + "\r\n");
            }
        }
    }
}

 

posted @ 2014-01-23 12:08  LS庆  阅读(368)  评论(0编辑  收藏  举报