LINQ模型对比全面剖析

开发 后端
这里介绍从LINQ模型上看出XElement的重要性。使用XElement不仅可以从头创建xml文件,还可以使用Load的方法从文件加载。

这里主要介绍DOM模型和LINQ模型操作XML的区别,包括介绍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不仅可以从头创建xml文件,还可以使用Load的方法从文件加载。还可以从数据库中取出所需元素,这就要用到LINQ TO SQL的东西了,同样可以从数组中取出元素。操作完成后可以使用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模型能实现的LINQ模型就一定能实现。插入的时候还可以使用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是晚绑定语言,可以充分发挥它的优势。

【编辑推荐】

  1. Linq匿名类型简单概述
  2. Linq随机读取数据浅析
  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-16 16:59:05

LINQ to XML

2009-09-10 14:37:57

LINQ匿名类型

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-16 17:21:53

LINQ遍历

2009-09-11 12:13:40

LINQ to SQL

2009-09-18 16:20:36

LINQ基础

2009-09-16 10:38:43

LINQ查询

2009-09-09 13:39:05

Linq用户定义函数

2009-09-14 10:35:15

Linq内部执行原理

2009-09-16 09:56:42

LINQ to SQL

2009-09-08 15:39:13

Linq使用Inser

2009-09-17 09:20:34

Linq和dLinq区

2009-09-15 14:52:15

linq级联删除
点赞
收藏

51CTO技术栈公众号