国产欧美一区二区精品行性色_91精品午夜在线观看_亚洲精品无码激情国产_91精品啪在线观看国产城中村_91看片国产一区二区色欲

始創(chuàng)于2000年 股票代碼:831685
咨詢熱線:0371-60135900 注冊有禮 登錄
  • 掛牌上市企業(yè)
  • 60秒人工響應
  • 99.99%連通率
  • 7*24h人工
  • 故障100倍補償
全部產(chǎn)品
您的位置: 網(wǎng)站首頁 > 幫助中心>文章內(nèi)容

Linq to xml操作XML

發(fā)布時間:  2012/8/20 17:41:27

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

1. 使用linq to xml寫xml:

使用XDocument的構(gòu)造函數(shù)可以構(gòu)造一個Xml文檔對象;使用XElement對象可以構(gòu)造一個xml節(jié)點元素,使用XAttribute構(gòu)造函數(shù)可以構(gòu)造元素的屬性;使用XText構(gòu)造函數(shù)可以構(gòu)造節(jié)點內(nèi)的文本。

如下實例代碼:

  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是系統(tǒng)默認編碼,對于簡體中文操作系統(tǒng)是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屬性獲得節(jié)點的文本值;使用linq就可以方便的做查詢,做篩選排序了

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

  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是系統(tǒng)默認編碼,對于簡體中文操作系統(tǒng)是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篇博客信息

實現(xiàn)要點: 通過XDocument的Load靜態(tài)方法載入Xml,


本文出自:億恩科技【www.cmtents.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屬性獲得節(jié)點的文本值;使用linq就可以方便的做查詢,做篩選排序了

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

  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是系統(tǒng)默認編碼,對于簡體中文操作系統(tǒng)是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篇博客信息

實現(xiàn)要點: 通過XDocument的Load靜態(tài)方法載入Xml,


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

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

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

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