自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

LINQ模型對比全面剖析

開發(fā) 后端
這里介紹從LINQ模型上看出XElement的重要性。使用XElement不僅可以從頭創(chuàng)建xml文件,還可以使用Load的方法從文件加載。

這里主要介紹DOM模型和LINQ模型操作XML的區(qū)別,包括介紹LINQ模型上看出XElement的重要性和使用LINQ操作XML。

下面用代碼對比一下:

  1. //DOM模型  
  2. XmlDocument doc = new XmlDocument();  
  3. XmlElement name = doc.CreateElement("name");  
  4. name.InnerText = "Patrick Hines";  
  5. XmlElement phone1 = doc.CreateElement("phone");  
  6. phone1.SetAttribute("type", "home");  
  7. phone1.InnerText = "206-555-0144";         
  8. XmlElement phone2 = doc.CreateElement("phone");  
  9. phone2.SetAttribute("type", "work");  
  10. phone2.InnerText = "425-555-0145";         
  11. XmlElement street1 = doc.CreateElement("street1");         
  12. street1.InnerText = "123 Main St" 
  13. XmlElement city = doc.CreateElement("city");  
  14. city.InnerText = "Mercer Island";  
  15. XmlElement state = doc.CreateElement("state");  
  16. state.InnerText = "WA";  
  17. XmlElement postal = doc.CreateElement("postal");  
  18. postal.InnerText = "68042";  
  19. XmlElement address = doc.CreateElement("address");  
  20. address.AppendChild(street1);  
  21. address.AppendChild(city);  
  22. address.AppendChild(state);  
  23. address.AppendChild(postal)  
  24. XmlElement contact = doc.CreateElement("contact");  
  25. contact.AppendChild(name);  
  26. contact.AppendChild(phone1);  
  27. contact.AppendChild(phone2);  
  28. contact.AppendChild(address);  
  29. XmlElement contacts = doc.CreateElement("contacts");  
  30. contacts.AppendChild(contact);  
  31. doc.AppendChild(contacts); 

 

  1. //LINQ模型  
  2. XElement contacts =  
  3. new XElement("contacts",  
  4. new XElement("contact",  
  5. new XElement("name", "Patrick Hines"),  
  6. new XElement("phone", "206-555-0144",  
  7. new XAttribute("type", "home")),  
  8. new XElement("phone", "425-555-0145"  
  9. new XAttribute("type", "work")),  
  10. new XElement("address",  
  11. new XElement("street1", "123 Main St"),  
  12. new XElement("city", "Mercer Island"),  
  13. new XElement("state", "WA"),  
  14. new XElement("postal", "68042")  
  15. )  
  16. )  
  17. ); 

從對比上我們也可以看出LINQ模型的簡單性。我們還可以從LINQ模型上看出XElement的重要性。使用XElement不僅可以從頭創(chuàng)建xml文件,還可以使用Load的方法從文件加載。還可以從數(shù)據(jù)庫中取出所需元素,這就要用到LINQ TO SQL的東西了,同樣可以從數(shù)組中取出元素。操作完成后可以使用Save方法進行保存。

下面簡單介紹一下增刪查改XML。

  1. //查詢  
  2. foreach (c in contacts.Nodes()) ...{  
  3. Console.WriteLine(c);  

我們看到在輸出XML元素的時候并不需要對每個元素進行強制的類型轉換,這里C#編譯器已經做了這些事情,它會在輸出的時候調用每個元素的ToString()方法。

  1. //插入元素  
  2. XElement mobilePhone = new XElement("phone", "206-555-0168");  
  3. contact.Add(mobilePhone); 

這里只是很簡單的演示一些操作,至于那些復雜的操作,只要DOM模型能實現(xiàn)的LINQ模型就一定能實現(xiàn)。插入的時候還可以使用AddAfterThis和AddBeforeThis等方法,提高效率。

  1. //刪除元素  
  2. contact.Element("phone").Remove();  
  3. //刪除某一具體元素  
  4. contact.Elements("phone").Remove();  
  5. //刪除一組元素  
  6. contacts.Element(contact").Element("address").RemoveContent();  
  7. //刪除某一元素內容  
  8. //刪除元素還可以使適用SetElement方法,把某一元素設置為null也就是刪除了這元素。  
  9. //修改元素  
  10. contact.Element("phone").ReplaceContent("425-555-0155");  
  11. //這里是修改***個phone元素的內容 

當然同樣可以使用SetElement方法,這里才是它的用武之地。

從上面簡單的介紹我們可以清楚的看到,使用LINQ操作XML是多么的簡單,這里使用的C#語法,如果要是使用VB.NET還會更簡單。有一些方法在VB.NET中可以使用但是在C#中卻沒有。畢竟VB.NET是晚綁定語言,可以充分發(fā)揮它的優(yōu)勢。

【編輯推薦】

  1. Linq匿名類型簡單概述
  2. Linq隨機讀取數(shù)據(jù)淺析
  3. Linq Lambda表達式全面分析
  4. Linq擴展方法簡單分析
  5. 初探Linq局部變量類型
責任編輯:佚名 來源: 百度空間
相關推薦

2009-09-09 14:40:43

Linq to sql

2009-09-17 13:15:20

LINQ查詢

2009-09-08 16:20:12

LINQ to SQL

2009-09-10 17:44:36

DOM模型INQ模型

2010-09-10 10:07:26

無線網絡對比

2009-09-09 16:21:31

Linq使用sqlme

2009-09-14 10:13:02

LINQ查詢操作

2009-09-14 15:12:40

LINQ to XML

2009-09-10 14:37:57

LINQ匿名類型

2009-09-16 16:59:05

LINQ to XML

2009-09-11 12:13:40

LINQ to SQL

2009-09-16 17:21:53

LINQ遍歷

2009-09-18 16:20:36

LINQ基礎

2009-09-16 10:38:43

LINQ查詢

2009-09-08 15:39:13

Linq使用Inser

2009-09-15 14:52:15

linq級聯(lián)刪除

2009-09-17 09:20:34

Linq和dLinq區(qū)

2009-09-16 09:56:42

LINQ to SQL

2009-09-09 13:39:05

Linq用戶定義函數(shù)

2009-09-14 10:35:15

Linq內部執(zhí)行原理
點贊
收藏

51CTO技術棧公眾號