成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

學習心得LINQ to XML

開發 后端
本文從六個方面對LINQ to XML做了簡單介紹,它們分別是命名空間、編程方式創建XML文檔、使用LINQ查詢創建XML文檔等等。

本文從六個方面對LINQ to XML做了簡單介紹,它們分別是命名空間、編程方式創建XML文檔、使用LINQ查詢創建XML文檔等等。

LINQ to XML可以看作是一個 “better DOM” 編程模型,可以和 System.Xml.dll 程序集中的很多成員交互。

一、命名空間

System.Xml.Linq.dll 程序集定義了三個命名空間:System.Xml.Linq, System.Xml.Schema  和 System.Xml.XPath

最核心的是 System.Xml.Linq, 定義了對應 XML 文檔個方面的很多類型

定義XML文檔類型 
定義XML文檔類型

二、編程方式創建XML文檔

以前的 .NET XML編程模型需要使用很多冗長的 DOM API,而 LINQ to XML 則完全可以用與 DOM 無關的方式與 XML 文檔交互。這樣不但大大減少了代碼行,而且這種編程模型可以直接映射到格式良好的XML文檔結構。

  1. static void CreateFunctionalXmlElement()  
  2. {  
  3. // A "functional" approach to build an  
  4. // XML element in memory.  
  5. XElement inventory =  
  6. new XElement("Inventory",  
  7. new XElement("Car", new XAttribute("ID", "1"),  
  8. new XElement("Color", "Green"),  
  9. new XElement("Make", "BMW"),  
  10. new XElement("PetName", "Stan")  
  11. )  
  12. );  
  13. // Call ToString() on our XElement.  
  14. Console.WriteLine(inventory);  

在內存中創建LINQ to XML文檔

  1. static void CreateFunctionalXmlDoc(  
  2.        {  
  3.            XDocument inventoryDoc =  
  4.            new XDocument(  
  5.            new XDeclaration("1.0", "utf-8", "yes"),  
  6.            new XComment("Current Inventory of AutoLot"),  
  7.            new XElement("Inventory",  
  8.            new XElement("Car", new XAttribute("ID", "1"),  
  9.            new XElement("Color", "Green"),  
  10.            new XElement("Make", "BMW"),  
  11.            new XElement("PetName", "Stan")  
  12.            ),  
  13.            new XElement("Car", new XAttribute("ID", "2"),  
  14.            new XElement("Color", "Pink"),  
  15.            new XElement("Make", "Yugo"),  
  16.            new XElement("PetName", "Melvin")  
  17.            )  
  18.            )  
  19.            );  
  20.            // Display the document and save to disk.  
  21.            Console.WriteLine(inventoryDoc);  
  22.            inventoryDoc.Save("SimpleInventory.xml");  
  23.        } 

三、使用LINQ查詢創建XML文檔

  1. static void CreateXmlDocFromArray()  
  2. {  
  3. // Create an anonymous array of anonymous types.  
  4. var data = new [] {  
  5. new { PetName = "Melvin"ID = 10 },  
  6. new { PetName = "Pat"ID = 11 },  
  7. new { PetName = "Danny"ID = 12 },  
  8. new { PetName = "Clunker"ID = 13 }  
  9. };  
  10. // Now enumerate over the array to build  
  11. // an XElement.  
  12. XElement vehicles =  
  13. new XElement("Inventory",  
  14. from c in data  
  15. select new XElement("Car",  
  16. new XAttribute("ID", c.ID),  
  17. new XElement("PetName", c.PetName)  
  18. )  
  19. );  
  20. Console.WriteLine(vehicles);  

四、加載和解析LINQ to XML內容

  1. static void LoadExistingXml()  
  2.         {  
  3.             // Build an XElement from string.  
  4.             string myElement =  
  5.                                         @"'3'>  
  6.                             Yellow  
  7.                             Yugo  
  8.                             ";  
  9.             XElement newElement = XElement.Parse(myElement);  
  10.             Console.WriteLine(newElement);  
  11.             Console.WriteLine();  
  12.             // Load the SimpleInventory.xml file.  
  13.             XDocument myDoc = XDocument.Load("SimpleInventory.xml");  
  14.             Console.WriteLine(myDoc);  
  15.         } 

五、遍歷內存中的LINQ to XML 文檔

LINQ to XML 示例:

  1. "1.0" encoding="utf-8"?>  
  2.  
  3.   "0">  
  4.     Ford  
  5.     Blue  
  6.     Chuck  
  7.     
  8.   "1">  
  9.     VW  
  10.     Silver  
  11.     Mary  
  12.     
  13.   "2">  
  14.     Yugo  
  15.     Pink  
  16.     Gipper  
  17.     
  18.   "55">  
  19.     Ford  
  20.     Yellow  
  21.     862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS  
  22.     Max  
  23.     
  24.   "98">  
  25.     BMW  
  26.     Black  
  27.     Zippy  
  28.     
  29.  

LINQ to XML 加載

  1. static void Main(string[] args)  
  2.         {  
  3.             Console.WriteLine("***** Fun with LINQ to XML *****\n");  
  4.             // Load the Inventory.xml document into memory.  
  5.             XElement doc = XElement.Load("Inventory.xml");  
  6.             // We will author each of these next  
  7.             PrintAllPetNames(doc);  
  8.             Console.WriteLine();  
  9.             GetAllFords(doc);  
  10.             Console.ReadLine();  
  11.         } 

LINQ to XML遍歷

  1. static void PrintAllPetNames(XElement doc)  
  2. {  
  3. var petNames = from pn in doc.Descendants("PetName")  
  4. select pn.Value;  
  5. foreach (var name in petNames)  
  6. Console.WriteLine("Name: {0}", name);  

LINQ to XML查詢

  1. static void GetAllFords(XElement doc)  
  2.         {  
  3.             var fords = from c in doc.Descendants("Make")  
  4.                         where c.Value == "Ford" 
  5.                         select c;  
  6.             foreach (var f in fords)  
  7.                 Console.WriteLine("Name: {0}", f);  
  8.         } 

六、修改LINQ to XML 文檔

  1. static void AddNewElements(XElement doc)  
  2. {  
  3. // Add 5 new purple Fords to the incoming document.  
  4. for (int i = 0; i < 5; i++)  
  5. {  
  6. // Create a new XElement  
  7. XElement newCar =  
  8. new XElement("Car"new XAttribute("ID", i + 1000),  
  9. new XElement("Color""Green"),  
  10. new XElement("Make""Ford"),  
  11. new XElement("PetName""")  
  12. );  
  13. // Add to doc.  
  14. doc.Add(newCar);  
  15. }  
  16. // Show the updates.  
  17. Console.WriteLine(doc);  

以上就是對LINQ to XML 的簡單介紹。

【編輯推薦】

  1. 詳談Linq查詢結果分析的方法
  2. 簡簡單單學習Linq查詢語法
  3. 詳細闡述Linq插入數據的操作方法
  4. 淺析Linq插入數據的實現方法
  5. 簡單解決Linq多條件組合問題
責任編輯:阡陌 來源: 博客園
相關推薦

2009-09-14 18:19:49

LINQ模糊查詢

2011-04-06 11:08:52

Java異常

2009-09-17 13:54:26

LINQ to XML

2009-06-23 09:05:10

Seam框架JSF

2009-12-22 15:49:50

Linux系統文件鎖

2009-11-06 17:31:03

Oracle密碼文件

2012-06-27 14:04:22

folly

2009-08-18 10:59:46

C#枚舉類型

2011-01-04 09:34:23

LambdaC#

2011-07-22 08:43:08

XML

2009-08-13 18:10:31

C#靜態構造函數

2009-09-10 16:35:06

CCNA考試培訓

2009-07-15 18:11:17

MVC模式學習

2009-09-17 13:30:32

LINQ to XML

2009-01-04 16:58:48

LINQ to XMLLINQXML

2009-09-14 15:12:40

LINQ to XML

2009-09-16 15:33:22

LINQ to XML

2009-09-16 16:52:50

LINQ to XML

2011-07-26 13:58:17

LINQ

2009-09-14 16:41:23

LINQ To XML
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品麻 | 天堂va在线观看 | 国产精品欧美一区二区三区不卡 | 欧美成人a | 久久国产精品免费一区二区三区 | 国产999在线观看 | 一级片视频免费 | 亚洲精品电影在线观看 | 亚洲91视频| 亚洲伊人精品酒店 | 中文字幕在线一区二区三区 | 国产高清精品一区二区三区 | 久久国产精品久久久久久 | 亚洲视频在线一区 | 天天艹 | 黑人巨大精品欧美一区二区免费 | jizz在线看片 | 中文字幕欧美一区二区 | 在线91| av毛片| 少妇特黄a一区二区三区88av | 国产一区二区三区 | 日本精品视频 | 亚洲人va欧美va人人爽 | 精品国产乱码久久久久久88av | 一区视频在线免费观看 | 一区二区三区四区不卡 | 欧美国产91| 日韩电影一区 | 国产成人综合久久 | 99久久久国产精品免费消防器 | 污免费网站 | 在线观看免费av网 | 欧美成人精品一区二区男人看 | 日本一二三区电影 | 日韩午夜在线播放 | 自拍偷拍第一页 | 蜜桃视频一区二区三区 | 亚洲精品91 | 九九久久国产 | 成人av在线播放 |