日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
學(xué)習(xí)心得LINQ to XML

本文從六個(gè)方面對(duì)LINQ to XML做了簡(jiǎn)單介紹,它們分別是命名空間、編程方式創(chuàng)建XML文檔、使用LINQ查詢創(chuàng)建XML文檔等等。

公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)建站推出福海免費(fèi)做網(wǎng)站回饋大家。

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

一、命名空間

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

最核心的是 System.Xml.Linq, 定義了對(duì)應(yīng) XML 文檔個(gè)方面的很多類型

 
定義XML文檔類型

二、編程方式創(chuàng)建XML文檔

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

 
 
 
 
  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);  

在內(nèi)存中創(chuàng)建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查詢創(chuàng)建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內(nèi)容

 
 
 
 
  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.         } 

五、遍歷內(nèi)存中的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);  

以上就是對(duì)LINQ to XML 的簡(jiǎn)單介紹。

【編輯推薦】

  1. 詳談Linq查詢結(jié)果分析的方法
  2. 簡(jiǎn)簡(jiǎn)單單學(xué)習(xí)Linq查詢語(yǔ)法
  3. 詳細(xì)闡述Linq插入數(shù)據(jù)的操作方法
  4. 淺析Linq插入數(shù)據(jù)的實(shí)現(xiàn)方法
  5. 簡(jiǎn)單解決Linq多條件組合問題

本文題目:學(xué)習(xí)心得LINQ to XML
轉(zhuǎn)載來(lái)于:http://m.5511xx.com/article/cogisdg.html