欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

在C#中怎么使用XPath

這篇文章給大家分享的是有關(guān)在C#中怎么使用XPath的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

鷹手營子網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護(hù)。成都創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

XPath可以快速定位到Xml中的節(jié)點或者屬性。XPath語法很簡單,但是強(qiáng)大夠用,它也是使用xslt的基礎(chǔ)知識。

示例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中的符號

符號


說明


示例


示例說明


/


表示從根節(jié)點開始選擇

/pets

選擇根節(jié)點pets

表示節(jié)點和子節(jié)點之間的間隔符

/pets/dog

選擇pets節(jié)點下的dog節(jié)點

//xx


表示從整個xml文檔中查找,而不考慮當(dāng)前節(jié)點位置

//price

選擇文檔中所有的price節(jié)點

.


單個英文半角句點表示選擇當(dāng)前節(jié)點

/pets/.

選擇pets節(jié)點

..


雙點,表示選擇父節(jié)點

/pets/dog[0]/..

表示pets節(jié)點,也就是第一個dog節(jié)點的父節(jié)點

@xx


表示選擇屬性

//dog/@color

表示選擇所有dog節(jié)點的color屬性集合

[…]


中括號表示選擇條件,括號內(nèi)為條件

//dog[@color=’white’]

所有color為white的dog節(jié)點

//dog[/price<100]

所有price字節(jié)點值小于100的dog節(jié)點

中括號內(nèi)數(shù)字為節(jié)點索引,類似c#等語言中的數(shù)組,數(shù)組下標(biāo)是從1開始的

//dog[1]

第1個dog節(jié)點

//dog[last()]

最后一個dog節(jié)點,last()是xPath內(nèi)置函數(shù)

|


單豎杠表示合并節(jié)點結(jié)合

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

color屬性為white的dog節(jié)點和color屬性為white的cat節(jié)點

*


星號表示任何名字的節(jié)點或者屬性

//dog/*

表示dog節(jié)點的所有子節(jié)點

//dog/@*

表示dog節(jié)點的所有屬性節(jié)點

2. XPath數(shù)學(xué)運算符

+ 加號表示加

-表示數(shù)字相減

*表示乘以

p表示除以,這里數(shù)學(xué)上的除號/已經(jīng)被用作節(jié)點之間分隔符了

mod表示取余

3. XPath邏輯運算符

=等于,相當(dāng)于c#中的 ==

!=不等于

>大于

>=大于等于

<小于

<=小于等于

and并且 與關(guān)系

or或者 或關(guān)系

4. XPath Axes從字面翻譯這個是XPath軸的意思,但根據(jù)我的理解這個翻譯成XPath節(jié)點關(guān)系運算關(guān)鍵字更合適,就是一組關(guān)鍵字加上::雙冒號表示和當(dāng)前節(jié)點有關(guān)系的一個或者一組節(jié)點.

使用語法: axisname::nodetest[predicate] 即軸名字::節(jié)點名字[取節(jié)點條件]

具體說明如下:

關(guān)鍵字

說明

示例

示例說明

ancestor

當(dāng)前節(jié)點的父祖節(jié)點

ancestor::pig

當(dāng)前節(jié)點的祖先節(jié)點中的pig節(jié)點

ancestor-or-self

當(dāng)前節(jié)點以及其父祖節(jié)點

ancestor::pig

attribute

當(dāng)前節(jié)點的所有屬性

attribute::weight

相當(dāng)于@weight,attribute::和@是等價的

child

當(dāng)前節(jié)點的所有字節(jié)點

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

選擇名字不是price的子節(jié)點

descendant

子孫節(jié)點

descendant::*[@*]

有屬性的子孫節(jié)點

descendant-or-self

子孫節(jié)點以及當(dāng)前節(jié)點

descendant-or-self::*

following

Xml文檔中當(dāng)前節(jié)點之后的所有節(jié)點

following::*

following-sibling

當(dāng)前節(jié)點的同父弟弟節(jié)點

following-sibling::

preceding

Xml文檔中當(dāng)前節(jié)點之前的所有節(jié)點

preceding::*

namespace

選取當(dāng)前節(jié)點的所有命名空間節(jié)點

namespace::*

parent

當(dāng)前節(jié)點的父節(jié)點

parent::

相當(dāng)于雙點..

preceding-sibling

當(dāng)前節(jié)點之后的同父兄節(jié)點

preceding-sibling::*

self

當(dāng)前節(jié)點

self::*

相當(dāng)于單點.

5. 常用的XPath函數(shù)介紹:

在XPath表達(dá)式中常用的函數(shù)有下面兩個:

position() 表示節(jié)點的序號例如 //cat[position() = 2] 表示取序號為2的dog節(jié)點
last() 表示取最后一個節(jié)點 //cat[last()] 
name() 表示當(dāng)前節(jié)點名字 /pets/*[name() != 'pig'] 表示/pets下名字不是pig的子節(jié)點

XPath的函數(shù)還有很多,包括字符串函數(shù),數(shù)字函數(shù)和時間函數(shù)等,具體可以參考w3的網(wǎng)站。

以上是XPath的語法,下面我們看下如何在.Net中使用XPath

在.Net中可以通過XPathDocument或者XmlDocument類使用XPath。XPathDocument是只讀的方式定位Xml節(jié)點或者屬性文本等,而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選擇需要的節(jié)點
            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 UseXPath2
{
    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節(jié)點下的dog字節(jié)點
                XmlNodeList nodeListAllDog = doc.SelectNodes("/pets/dog");

                //所有的price節(jié)點
                XmlNodeList allPriceNodes = doc.SelectNodes("//price");

                //取最后一個price節(jié)點
                XmlNode lastPriceNode = doc.SelectSingleNode("//price[last()]");

                //用雙點號取price節(jié)點的父節(jié)點
                XmlNode lastPriceParentNode = lastPriceNode.SelectSingleNode("..");

                //選擇weight*count=40的所有動物,使用通配符*
                XmlNodeList nodeList = doc.SelectNodes("/pets/*[@weight*@count=40]");

                //選擇除了pig之外的所有動物,使用name()函數(shù)返回節(jié)點名字
                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節(jié)點
                XmlNode theSecondDogNode = doc.SelectSingleNode("//dog[position() = 2]");

                //使用xpath ,axes 的 parent 取父節(jié)點
                XmlNode parentNode = theSecondDogNode.SelectSingleNode("parent::*");

                //使用xPath選擇第二個dog節(jié)點前面的所有dog節(jié)點
                XmlNodeList dogPresibling = theSecondDogNode.SelectNodes("preceding::dog");

                //取文檔的所有子孫節(jié)點price
                XmlNodeList childrenNodes = doc.SelectNodes("descendant::price");
            }

            Console.Read();
        }
    }
}

感謝各位的閱讀!關(guān)于“在C#中怎么使用XPath”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁題目:在C#中怎么使用XPath
分享URL:http://chinadenli.net/article40/geceho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、服務(wù)器托管App設(shè)計、網(wǎng)站制作、虛擬主機(jī)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站