DOM模型和LINQ模型对比

开发 后端
这里介绍DOM模型和LINQ模型对比,C#语言有一套在DOM模型下操作XML的类库。但是在LINQ模型出现以后,微软又重新做了一套关于XML的模型,而且操作起来同那套DOM模型没什么两样。

在向大家详细介绍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概述
责任编辑:佚名 来源: 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

2022-10-09 15:26:45

人工智能ML机器学习

2010-09-28 10:40:32

HTML DOM

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:43:37

DOM文档对象模型

2010-09-28 09:22:34

DOM模型Html

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模型

2022-02-16 09:29:06

领域模型贫血模型充血模型

2009-05-11 10:40:36

.NETLINQforeach
点赞
收藏

51CTO技术栈公众号