xPath在C#中运用

<? xml  version="1.0" encoding="utf-8" ?>
< pets >
   < cat  color="black" weight="10">
     < price >100</ price >
     < desc >this is a black cat</ desc >
   </ cat >
   < cat  color="white" weight="9">
     < price >80</ price >
     < desc >this is a white cat</ desc >
   </ cat >
   < cat  color="yellow" weight="15">
     < price >80</ price >
     < desc >this is a yellow cat</ desc >
   </ cat >
 
 
   < dog  color="black" weight="10">
     < price >100</ price >
     < desc >this is a black dog</ desc >
   </ dog >
   < dog  color="white" weight="9">
     < price >80</ price >
     < desc >this is a white dog</ desc >
   </ dog >
   < dog  color="yellow" weight="15">
     < price >80</ price >
     < desc >this is a yellow dog</ desc >
   </ dog >
</ pets >
1. XPath中的符号

符号

说明

示例

示例说明

/

表示从根节点开始选择

/pets

选择根节点pets

表示节点和子节点之间的间隔符

/pets/dog

选择pets节点下的dog节点

//xx

表示从整个xml文档中查找,而不考虑当前节点位置

//price

选择文档中所有的price节点

.

单个英文半角句点表示选择当前节点

/pets/.

选择pets节点

..

双点,表示选择父节点

/pets/dog[0]/..

表示pets节点,也就是第一个dog节点的父节点

@xx

表示选择属性

//dog/@color

表示选择所有dog节点的color属性集合

[…]

中括号表示选择条件,括号内为条件

//dog[@color=’white’]

所有color为white的dog节点

//dog[/price<100]

所有price字节点值小于100的dog节点

中括号内数字为节点索引,类似c#等语言中的数组,数组下标是从1开始的

//dog[1]

1个dog节点

//dog[last()]

最后一个dog节点,last()是xPath内置函数

|

单竖杠表示合并节点结合

//dog[@color=’white’] | //cat[@color=’white’]

color属性为white的dog节点和color属性为white的cat节点

*

星号表示任何名字的节点或者属性

//dog/*

表示dog节点的所有子节点

//dog/@*

表示dog节点的所有属性节点

 

2. XPath数学运算符
+  加号表示加
- 表示数字相减
* 表示乘以
div 表示除以,这里数学上的除号/已经被用作节点之间分隔符了
mod 表示取余
3. XPath逻辑运算符
= 等于,相当于c#中的 ==
!= 不等于
> 大于
>= 大于等于
< 小于
<= 小于等于
and 并且 与关系
or 或者 或关系
4.  XPath Axes 从字面翻译这个是XPath轴的意思,但根据我的理解这个翻译成XPath节点关系运算关键字更合适,就是一组关键字加上::双冒号表示和当前节点有关系的一个或者一组节点.
使用语法: axisname::nodetest[predicate] 即轴名字::节点名字[取节点条件]
具体说明如下:
关键字

说明

示例

示例说明

ancestor

当前节点的父祖节点

ancestor::pig

当前节点的祖先节点中的pig节点

ancestor-or-self

当前节点以及其父祖节点

ancestor::pig

 

attribute

当前节点的所有属性

attribute::weight

相当于@weight,attribute::和@是等价的

child

当前节点的所有字节点

child::*[name()!=’price’]

选择名字不是price的子节点

descendant

子孙节点

descendant::*[@*]

有属性的子孙节点

descendant-or-self

子孙节点以及当前节点

descendant-or-self::*

 

following

Xml文档中当前节点之后的所有节点

following::*

 

following-sibling

当前节点的同父弟弟节点

following-sibling::

 

preceding

Xml文档中当前节点之前的所有节点

preceding::*

 

namespace

选取当前节点的所有命名空间节点

namespace::*

 

parent

当前节点的父节点

parent::

相当于双点..

preceding-sibling

当前节点之后的同父兄节点

preceding-sibling::*

 

self

当前节点

self::*

相当于单点.

 
 
实例:
查询pets下的cat节点,条件是price=100
这时写xpath语句时要注意,错误写法:
var price = 100;
var xPath = "pets/cat[price="+price+"]";
 
正确写法:
①、
var price = 100;
var xPath = string.Format("pets/cat[price='{0}']",price );
 
②、
var xPath = string.Format("pets/cat[price=100]" );

转载于:https://www.cnblogs.com/joyang/p/4998635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值