在c#中,我们常常查找,遍历节点,这是我们能够用到XPath语法,XPath语法很简单,然则壮大够用,XPath能够疾速定位到Xml中的节点或许属性,它也是运用xslt的基础知识
示例Xml:
<?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>
XPath的语法:
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数学运算符
+ 加号示意加
- 示意数字相减
* 示意乘以
p 示意除以,这里数学上的除号/已被用作节点之间分隔符了
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::* |
相当于单点. |
5. 经常使用的XPath函数引见:
在XPath表达式中经常使用的函数有下面两个:
position() 示意节点的序号比方 //cat[position() = 2] 示意取序号为2的dog节点
last() 示意取末了一个节点 //cat[last()]
name() 示意当前节点名字 /pets/*[name() != 'pig'] 示意/pets下名字不是pig的子节点
XPath的函数另有许多,包含字符串函数,数字函数和时候函数等,详细能够参考w3的网站。
以上是XPath的语法,下面我们看下如安在.Net中运用XPath
在.Net中能够经由过程XPathDocument或许XmlDocument类运用XPath。XPathDocument是只读的体式格局定位Xml节点或许属性文本等,而XmlDocument则是可读写的。
以下代码示例展现了怎样运用XPathDocument和XmlDocument。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.XPath; using System.Xml; namespace UseXPathDotNet { class Program { static void Main(string[] args) { UseXPathWithXPathDocument(); UseXPathWithXmlDocument(); Console.Read(); } static void UseXPathWithXmlDocument() { XmlDocument doc = new XmlDocument(); doc.Load("http://www.cnblogs.com/yukaizhao/rss"); //运用xPath挑选须要的节点 XmlNodeList nodes = doc.SelectNodes("/rss/channel/item[position()<=10]"); foreach (XmlNode item in nodes) { string title = item.SelectSingleNode("title").InnerText; string url = item.SelectSingleNode("link").InnerText; Console.WriteLine("{0} = {1}", title, url); } } static void UseXPathWithXPathDocument() { XPathDocument doc = new XPathDocument("http://www.cnblogs.com/yukaizhao/rss"); XPathNavigator xPathNav = doc.CreateNavigator(); //运用xPath取rss中最新的10条漫笔 XPathNodeIterator nodeIterator = xPathNav.Select("/rss/channel/item[position()<=10]"); while (nodeIterator.MoveNext()) { XPathNavigator itemNav = nodeIterator.Current; string title = itemNav.SelectSingleNode("title").Value; string url = itemNav.SelectSingleNode("link").Value; Console.WriteLine("{0} = {1}",title,url); } } } }
XPath运用示例,请看下面的代码解释
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; namespace UseXPath1 { class Program { static void Main(string[] args) { string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> <pets> <cat color=""black"" weight=""10"" count=""4""> <price>100</price> <desc>this is a black cat</desc> </cat> <cat color=""white"" weight=""9"" count=""5""> <price>80</price> <desc>this is a white cat</desc> </cat> <cat color=""yellow"" weight=""15"" count=""1""> <price>110</price> <desc>this is a yellow cat</desc> </cat> <dog color=""black"" weight=""10"" count=""7""> <price>114</price> <desc>this is a black dog</desc> </dog> <dog color=""white"" weight=""9"" count=""4""> <price>80</price> <desc>this is a white dog</desc> </dog> <dog color=""yellow"" weight=""15"" count=""15""> <price>80</price> <desc>this is a yellow dog</desc> </dog> <pig color=""white"" weight=""100"" count=""2""> <price>8000</price> <desc>this is a white pig</desc> </pig> </pets>"; using (StringReader rdr = new StringReader(xml)) { XmlDocument doc = new XmlDocument(); doc.Load(rdr); //取一切pets节点下的dog字节点 XmlNodeList nodeListAllDog = doc.SelectNodes("/pets/dog"); //一切的price节点 XmlNodeList allPriceNodes = doc.SelectNodes("//price"); //取末了一个price节点 XmlNode lastPriceNode = doc.SelectSingleNode("//price[last()]"); //用双点号取price节点的父节点 XmlNode lastPriceParentNode = lastPriceNode.SelectSingleNode(".."); //挑选weight*count=40的一切动物,运用通配符* XmlNodeList nodeList = doc.SelectNodes("/pets/*[@weight*@count=40]"); //挑选除了pig以外的一切动物,运用name()函数返回节点名字 XmlNodeList animalsExceptPigNodes = doc.SelectNodes("/pets/*[name() != 'pig']"); //挑选价钱大于100而不是pig的动物 XmlNodeList priceGreaterThan100s = doc.SelectNodes("/pets/*[price p @weight >10 and name() != 'pig']"); foreach (XmlNode item in priceGreaterThan100s) { Console.WriteLine(item.OuterXml); } //挑选第二个dog节点 XmlNode theSecondDogNode = doc.SelectSingleNode("//dog[position() = 2]"); //运用xpath ,axes 的 parent 取父节点 XmlNode parentNode = theSecondDogNode.SelectSingleNode("parent::*"); //运用xPath挑选第二个dog节点前面的一切dog节点 XmlNodeList dogPresibling = theSecondDogNode.SelectNodes("preceding::dog"); //取文档的一切子孙节点price XmlNodeList childrenNodes = doc.SelectNodes("descendant::price"); } Console.Read(); } } }
以上就是xml进修(6) 在c#Xpath实例的内容,更多相关内容请关注ki4网(www.ki4.cn)!