• <ul id="mayc0"></ul>
    <ul id="mayc0"><center id="mayc0"></center></ul>
    <strike id="mayc0"><input id="mayc0"></input></strike>
    <ul id="mayc0"></ul>
  • 始創于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊有禮 登錄
    • 掛牌上市企業
    • 60秒人工響應
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補償
    您的位置: 網站首頁 > 幫助中心>文章內容

    Linq to xml操作XML

    發布時間:  2012/8/20 17:41:27

    .Net中的System.Xml.Linq命名空間提供了linq to xml的支持。這個命名空間中的XDocument,XElement以及XText,XAttribute提供了讀寫xml文檔的關鍵方法。

    1. 使用linq to xml寫xml:

    使用XDocument的構造函數可以構造一個Xml文檔對象;使用XElement對象可以構造一個xml節點元素,使用XAttribute構造函數可以構造元素的屬性;使用XText構造函數可以構造節點內的文本。

    如下實例代碼:

    1. class Program  
    2. {  
    3.     static void Main(string[] args)  
    4.     {             
    5.         var xDoc = new XDocument(new XElement( "root",  
    6.             new XElement("dog",  
    7.                 new XText("dog said black is a beautify color"),  
    8.                 new XAttribute("color", "black")),  
    9.             new XElement("cat"),  
    10.             new XElement("pig", "pig is great")));  
    11.  
    12.         //xDoc輸出xml的encoding是系統默認編碼,對于簡體中文操作系統是gb2312  
    13.         //默認是縮進格式化的xml,而無須格式化設置  
    14.         xDoc.Save(Console.Out);  
    15.  
    16.         Console.Read();  
    17.     }  

    上面代碼將輸出如下Xml:

    1. <?xml version="1.0" encoding="gb2312"?> 
    2. <root> 
    3.   <dog color="black">dog said black is a beautify color</dog> 
    4.   <cat /> 
    5.   <pig>pig is great</pig> 
    6. </root> 

     

    可以看出linq to xml比XmlDocument和XmlWriter要方便很多。

    2. 使用linq to xml 讀取xml

    Linq是從集合中查詢對象,在linq to xml中的集合是通過XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個重載方法中獲得。

    獲得XElement集合之后,可以通過XElement的Attribute(string name)方法獲得元素的屬性值,可以通過XElement的Value屬性獲得節點的文本值;使用linq就可以方便的做查詢,做篩選排序了

    還是上例中的xml,我們要讀取root的所有字節點,并打印出來,如下代碼:

    1. class Program  
    2. {  
    3.     static void Main(string[] args)  
    4.     {  
    5.              
    6.         var xDoc = new XDocument(new XElement( "root",  
    7.             new XElement("dog",  
    8.                 new XText("dog said black is a beautify color"),  
    9.                 new XAttribute("color", "black")),  
    10.             new XElement("cat"),  
    11.             new XElement("pig", "pig is great")));  
    12.  
    13.         //xDoc輸出xml的encoding是系統默認編碼,對于簡體中文操作系統是gb2312  
    14.         //默認是縮進格式化的xml,而無須格式化設置  
    15.         xDoc.Save(Console.Out);  
    16.  
    17.         Console.WriteLine();  
    18.  
    19.         var query = from item in xDoc.Element( "root").Elements()  
    20.                     select new  
    21.                     {  
    22.                         TypeName    = item.Name,  
    23.                         Saying      = item.Value,  
    24.                         Color       = item.Attribute("color") == null?(string)null:item.Attribute("color").Value  
    25.                     };  
    26.  
    27.  
    28.         foreach (var item in query)  
    29.         {  
    30.             Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");  
    31.         }  
    32.  
    33.         Console.Read();  
    34.     }  

    3. Linq to xml簡單的應用

    應用需求: 讀取博客園的rss,然后在頁面上輸出最新的10篇博客信息

    實現要點: 通過XDocument的Load靜態方法載入Xml,


    本文出自:億恩科技【www.vbseamall.com】

     

    可以看出linq to xml比XmlDocument和XmlWriter要方便很多。

    2. 使用linq to xml 讀取xml

    Linq是從集合中查詢對象,在linq to xml中的集合是通過XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的幾個重載方法中獲得。

    獲得XElement集合之后,可以通過XElement的Attribute(string name)方法獲得元素的屬性值,可以通過XElement的Value屬性獲得節點的文本值;使用linq就可以方便的做查詢,做篩選排序了

    還是上例中的xml,我們要讀取root的所有字節點,并打印出來,如下代碼:

    1. class Program  
    2. {  
    3.     static void Main(string[] args)  
    4.     {  
    5.              
    6.         var xDoc = new XDocument(new XElement( "root",  
    7.             new XElement("dog",  
    8.                 new XText("dog said black is a beautify color"),  
    9.                 new XAttribute("color", "black")),  
    10.             new XElement("cat"),  
    11.             new XElement("pig", "pig is great")));  
    12.  
    13.         //xDoc輸出xml的encoding是系統默認編碼,對于簡體中文操作系統是gb2312  
    14.         //默認是縮進格式化的xml,而無須格式化設置  
    15.         xDoc.Save(Console.Out);  
    16.  
    17.         Console.WriteLine();  
    18.  
    19.         var query = from item in xDoc.Element( "root").Elements()  
    20.                     select new  
    21.                     {  
    22.                         TypeName    = item.Name,  
    23.                         Saying      = item.Value,  
    24.                         Color       = item.Attribute("color") == null?(string)null:item.Attribute("color").Value  
    25.                     };  
    26.  
    27.  
    28.         foreach (var item in query)  
    29.         {  
    30.             Console.WriteLine("{0} 's color is {1},{0} said {2}",item.TypeName,item.Color??"Unknown",item.Saying??"nothing");  
    31.         }  
    32.  
    33.         Console.Read();  
    34.     }  

    3. Linq to xml簡單的應用

    應用需求: 讀取博客園的rss,然后在頁面上輸出最新的10篇博客信息

    實現要點: 通過XDocument的Load靜態方法載入Xml,


    本文出自:億恩科技【www.enidc.com】
    -->

    服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
      0
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線