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

DOM模型和LINQ模型對比

開發(fā) 后端
這里介紹DOM模型和LINQ模型對比,C#語言有一套在DOM模型下操作XML的類庫。但是在LINQ模型出現(xiàn)以后,微軟又重新做了一套關于XML的模型,而且操作起來同那套DOM模型沒什么兩樣。

在向大家詳細介紹LINQ模型之前,首先讓大家了解下DOM模型,然后全面介紹LINQ模型

DOM模型和LINQ模型

我們知道關于XML,W3C有一套DOM模型,C#語言有一套在DOM模型下操作XML的類庫。但是在LINQ模型出現(xiàn)以后,微軟又重新做了一套關于XML的模型,而且操作起來同那套DOM模型沒什么兩樣,但是更加的簡單。

DOM模型

以上是一套新的類庫。其中最核心的類就是XElement,不要看它的層次低,但是絕對是核心。還有一些其他特性與DOM模型不一樣,其中之一就是XAttribute和XNode在同一個層次上,還有就是XDocument不再是必須的。其他不同點可以參考DOM模型自己比較。

下面用代碼對比一下DOM模型和LINQ模型操作XML的區(qū)別:

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

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

【編輯推薦】

  1. LINQ to SQL Table淺談
  2. Linq語句問題的解決方法
  3. Ling to sql更新實體概述
  4. Linq實體繼承簡單描述
  5. Linq Library概述
責任編輯:佚名 來源: IT168
相關推薦

2009-09-18 17:17:58

LINQ模型

2010-09-28 09:33:25

DOM模型

2009-09-08 16:20:12

LINQ to SQL

2009-09-15 10:12:37

LINQ To SQL

2010-09-28 10:40:32

HTML DOM

2022-10-09 15:26:45

人工智能ML機器學習

2009-09-18 14:07:51

LINQ to SQL

2010-09-28 10:03:15

DOM文檔對象模型

2012-04-26 08:29:22

DOM

2010-09-28 09:22:34

DOM模型Html

2010-09-28 09:43:37

DOM文檔對象模型

2010-09-28 13:24:34

DOM文檔對象模型

2009-09-14 13:50:35

LINQ編程模型

2020-10-15 11:22:34

PyTorchTensorFlow機器學習

2009-02-10 09:23:03

DOM模型MSXML

2010-09-28 11:03:19

XML DOM

2010-09-28 10:09:35

DOM對象模型

2010-09-28 09:38:22

DOM模型

2009-05-11 10:40:36

.NETLINQforeach

2022-02-16 09:29:06

領域模型貧血模型充血模型
點贊
收藏

51CTO技術棧公眾號