XDocument讀取xml的所有元素以及XPath語法


<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
  
</bookstore>

以上是BookStore.xml文件

接着開始處理

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字
            IEnumerable<XElement> elements = xroot.Elements();//獲得根節點下的元素集合     
            foreach (XElement item in elements)
            {
          Console.WriteLine(item.Name); DiGuiNode(item);
//遞歸獲得此元素下的子元素 }
        private static void DiGuiNode(XElement xroot)
        {
            if (xroot!=null)
            {
                foreach (var item in xroot.Elements())
                {
                    Console.WriteLine(item.Name);//獲得元素名
                    Console.WriteLine(item.Value);//獲得元素的基本值
                    DiGuiNode(item);
                }
            }
        }    

運行結果:

2.XPath語法:

表達式 描述
nodename 選取此節點的所有直接子nodename節點。
/ 從根節點選取。
// 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。
. 選取當前節點。
.. 選取當前節點的父節點。
@ 選取屬性。

2.1

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字            
            IEnumerable<XElement> elements = xroot.XPathSelectElements("book");//找根節點的直接book子節點
            foreach (XElement item in elements)
            {
                Console.WriteLine(item.Name);                
            }                

運行結果:

2.2 假如路徑起始於正斜杠( / ),則此路徑始終代表到某元素的絕對路徑!

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點           
            IEnumerable<XElement> elements = xroot.XPathSelectElements("/bookstore/book/price");//假如路徑起始於正斜杠( / ),則此路徑始終代表到某元素的絕對路徑!
            foreach (XElement item in elements)
            {
                Console.WriteLine(item.Name);               
            }                

運行結果:

2.3  // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字            
            IEnumerable<XElement> elements = xroot.XPathSelectElements("//author");
            foreach (XElement item in elements)
            {
                Console.WriteLine(item.Name);             
            }            

運行結果:

2.4  .表示選取當前節點。

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字
           
            IEnumerable<XElement> elements = xroot.XPathSelectElements(".");//.表示當前節點,此處表示根節點
            foreach (XElement item in elements)
            {
                Console.WriteLine(item.Name);            
            }            

運行結果:

2.5  ..表示選取當前節點的父節點。

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字           
            IEnumerable<XElement> elements = xroot.XPathSelectElements("book");//找根節點的直接book子節點          
            foreach (XElement item in elements)
            {
                XElement element = item.XPathSelectElement("..");//..表示選取當前節點的父節點,此處獲得的是item表示book節點,所以element就表示bookstore節點
                Console.WriteLine(element.Name);               
            }            

運行結果:

2.6 @表示選取帶此屬性的元素

            XDocument xdoc = XDocument.Load("../../BookStore.xml");//加載xml文件
            XElement xroot = xdoc.Root;//獲取根節點
            Console.WriteLine(xroot.Name);//輸出根節點的名字        
            IEnumerable<XElement> elements = xroot.XPathSelectElements("//book[@category='WEB']");//表示選擇出含有值為WEB的category屬性的book元素
            foreach (XElement item in elements)
            {                             
                Console.WriteLine(item.Name);      
            }                

運行結果:

謂語(Predicates)

謂語用來查找某個特定的節點或者包含某個指定的值的節點。

謂語被嵌在方括號中。

實例

在下面的表格中,我們列出了帶有謂語的一些路徑表達式,以及表達式的結果:

路徑表達式 結果
/bookstore/book[1] 選取屬於 bookstore 子元素的第一個 book 元素。
/bookstore/book[last()] 選取屬於 bookstore 子元素的最后一個 book 元素。
/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。
/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
//title[@lang] 選取所有擁有名為 lang 的屬性的 title 元素。
//title[@lang='eng'] 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。
/bookstore/book[price>35.00] 選取 bookstore 元素的所有 book 元素,且其中的 price 元素的值須大於 35.00。
/bookstore/book[price>35.00]/title 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大於 35.00。

選取未知節點

XPath 通配符可用來選取未知的 XML 元素。

通配符 描述
* 匹配任何元素節點。
@* 匹配任何屬性節點。
node() 匹配任何類型的節點。

實例

在下面的表格中,我們列出了一些路徑表達式,以及這些表達式的結果:

路徑表達式 結果
/bookstore/* 選取 bookstore 元素的所有子元素。
//* 選取文檔中的所有元素。
//title[@*] 選取所有帶有屬性的 title 元素。

選取若干路徑

通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑。

實例

在下面的表格中,我們列出了一些路徑表達式,以及這些表達式的結果:

路徑表達式 結果
//book/title | //book/price 選取 book 元素的所有 title 和 price 元素。
//title | //price 選取文檔中的所有 title 和 price 元素。
/bookstore/book/title | //price 選取屬於 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。

以上表格信息摘自http://www.w3school.com.cn/xpath/xpath_syntax.asp
      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM