DOM模型和LINQ模型对比
创始人
2024-06-06 18:40:17
0

在向大家详细介绍LINQ模型之前,首先让大家了解下DOM模型,然后全面介绍LINQ模型

DOM模型和LINQ模型

我们知道关于XML,W3C有一套DOM模型,C#语言有一套在DOM模型下操作XML的类库。但是在LINQ模型出现以后,微软又重新做了一套关于XML的模型,而且操作起来同那套DOM模型没什么两样,但是更加的简单。

DOM模型

以上是一套新的类库。其中最核心的类就是XElement,不要看它的层次低,但是绝对是核心。还有一些其他特性与DOM模型不一样,其中之一就是XAttribute和XNode在同一个层次上,还有就是XDocument不再是必须的。其他不同点可以参考DOM模型自己比较。

下面用代码对比一下DOM模型和LINQ模型操作XML的区别:

  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模型能实现的LINQ模型就一定能实现。插入的时候还可以使用AddAfterThis和AddBeforeThis等方法,提高效率。

【编辑推荐】

  1. LINQ to SQL Table浅谈
  2. Linq语句问题的解决方法
  3. Ling to sql更新实体概述
  4. Linq实体继承简单描述
  5. Linq Library概述

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...