学习心得LINQ to XML
创始人
2024-06-07 09:11:15
0

本文从六个方面对LINQ to XML做了简单介绍,它们分别是命名空间、编程方式创建XML文档、使用LINQ查询创建XML文档等等。

LINQ to XML可以看作是一个 “better DOM” 编程模型,可以和 System.Xml.dll 程序集中的很多成员交互。

一、命名空间

System.Xml.Linq.dll 程序集定义了三个命名空间:System.Xml.Linq, System.Xml.Schema  和 System.Xml.XPath

最核心的是 System.Xml.Linq, 定义了对应 XML 文档个方面的很多类型

定义XML文档类型 
定义XML文档类型

二、编程方式创建XML文档

以前的 .NET XML编程模型需要使用很多冗长的 DOM API,而 LINQ to XML 则完全可以用与 DOM 无关的方式与 XML 文档交互。这样不但大大减少了代码行,而且这种编程模型可以直接映射到格式良好的XML文档结构。

  1. static void CreateFunctionalXmlElement()  
  2. {  
  3. // A "functional" approach to build an  
  4. // XML element in memory.  
  5. XElement inventory =  
  6. new XElement("Inventory",  
  7. new XElement("Car", new XAttribute("ID", "1"),  
  8. new XElement("Color", "Green"),  
  9. new XElement("Make", "BMW"),  
  10. new XElement("PetName", "Stan")  
  11. )  
  12. );  
  13. // Call ToString() on our XElement.  
  14. Console.WriteLine(inventory);  

在内存中创建LINQ to XML文档

  1. static void CreateFunctionalXmlDoc(  
  2.        {  
  3.            XDocument inventoryDoc =  
  4.            new XDocument(  
  5.            new XDeclaration("1.0", "utf-8", "yes"),  
  6.            new XComment("Current Inventory of AutoLot"),  
  7.            new XElement("Inventory",  
  8.            new XElement("Car", new XAttribute("ID", "1"),  
  9.            new XElement("Color", "Green"),  
  10.            new XElement("Make", "BMW"),  
  11.            new XElement("PetName", "Stan")  
  12.            ),  
  13.            new XElement("Car", new XAttribute("ID", "2"),  
  14.            new XElement("Color", "Pink"),  
  15.            new XElement("Make", "Yugo"),  
  16.            new XElement("PetName", "Melvin")  
  17.            )  
  18.            )  
  19.            );  
  20.            // Display the document and save to disk.  
  21.            Console.WriteLine(inventoryDoc);  
  22.            inventoryDoc.Save("SimpleInventory.xml");  
  23.        } 

三、使用LINQ查询创建XML文档

  1. static void CreateXmlDocFromArray()  
  2. {  
  3. // Create an anonymous array of anonymous types.  
  4. var data = new [] {  
  5. new { PetName = "Melvin", ID = 10 },  
  6. new { PetName = "Pat", ID = 11 },  
  7. new { PetName = "Danny", ID = 12 },  
  8. new { PetName = "Clunker", ID = 13 }  
  9. };  
  10. // Now enumerate over the array to build  
  11. // an XElement.  
  12. XElement vehicles =  
  13. new XElement("Inventory",  
  14. from c in data  
  15. select new XElement("Car",  
  16. new XAttribute("ID", c.ID),  
  17. new XElement("PetName", c.PetName)  
  18. )  
  19. );  
  20. Console.WriteLine(vehicles);  

四、加载和解析LINQ to XML内容

  1. static void LoadExistingXml()  
  2.         {  
  3.             // Build an XElement from string.  
  4.             string myElement =  
  5.                                         @"'3'>  
  6.                             Yellow  
  7.                             Yugo  
  8.                             ";  
  9.             XElement newElement = XElement.Parse(myElement);  
  10.             Console.WriteLine(newElement);  
  11.             Console.WriteLine();  
  12.             // Load the SimpleInventory.xml file.  
  13.             XDocument myDoc = XDocument.Load("SimpleInventory.xml");  
  14.             Console.WriteLine(myDoc);  
  15.         } 

五、遍历内存中的LINQ to XML 文档

LINQ to XML 示例:

  1.  
  2.  
  3.   "0">  
  4.     Ford  
  5.     Blue  
  6.     Chuck  
  7.     
  8.   "1">  
  9.     VW  
  10.     Silver  
  11.     Mary  
  12.     
  13.   "2">  
  14.     Yugo  
  15.     Pink  
  16.     Gipper  
  17.     
  18.   "55">  
  19.     Ford  
  20.     Yellow  
  21.     862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS  
  22.     Max  
  23.     
  24.   "98">  
  25.     BMW  
  26.     Black  
  27.     Zippy  
  28.     
  29.  

LINQ to XML 加载

  1. static void Main(string[] args)  
  2.         {  
  3.             Console.WriteLine("***** Fun with LINQ to XML *****\n");  
  4.             // Load the Inventory.xml document into memory.  
  5.             XElement doc = XElement.Load("Inventory.xml");  
  6.             // We will author each of these next  
  7.             PrintAllPetNames(doc);  
  8.             Console.WriteLine();  
  9.             GetAllFords(doc);  
  10.             Console.ReadLine();  
  11.         } 

LINQ to XML遍历

  1. static void PrintAllPetNames(XElement doc)  
  2. {  
  3. var petNames = from pn in doc.Descendants("PetName")  
  4. select pn.Value;  
  5. foreach (var name in petNames)  
  6. Console.WriteLine("Name: {0}", name);  

LINQ to XML查询

  1. static void GetAllFords(XElement doc)  
  2.         {  
  3.             var fords = from c in doc.Descendants("Make")  
  4.                         where c.Value == "Ford" 
  5.                         select c;  
  6.             foreach (var f in fords)  
  7.                 Console.WriteLine("Name: {0}", f);  
  8.         } 

六、修改LINQ to XML 文档

  1. static void AddNewElements(XElement doc)  
  2. {  
  3. // Add 5 new purple Fords to the incoming document.  
  4. for (int i = 0; i < 5; i++)  
  5. {  
  6. // Create a new XElement  
  7. XElement newCar =  
  8. new XElement("Car", new XAttribute("ID", i + 1000),  
  9. new XElement("Color", "Green"),  
  10. new XElement("Make", "Ford"),  
  11. new XElement("PetName", "")  
  12. );  
  13. // Add to doc.  
  14. doc.Add(newCar);  
  15. }  
  16. // Show the updates.  
  17. Console.WriteLine(doc);  

以上就是对LINQ to XML 的简单介绍。

【编辑推荐】

  1. 详谈Linq查询结果分析的方法
  2. 简简单单学习Linq查询语法
  3. 详细阐述Linq插入数据的操作方法
  4. 浅析Linq插入数据的实现方法
  5. 简单解决Linq多条件组合问题

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...