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.5lwq4hdr.cn");
//使用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.5lwq4hdr.cn");
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("http://price");
//取最后一個price節點
XmlNode lastPriceNode = doc.SelectSingleNode("http://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 div @weight >10 and name() != 'pig']");
foreach (XmlNode item in priceGreaterThan100s)
{
Console.WriteLine(item.OuterXml);
}
//選擇第二個dog節點
XmlNode theSecondDogNode = doc.SelectSingleNode("http://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();
}
}
}