开发手册 欢迎您!
软件开发者资料库

LINQ - XML

LINQ XML - 从简单和简单的步骤学习LINQ(语言集成查询),从基本到高级概念,包括概述,环境设置,标准查询运算符,LINQ to SQL,LINQ对象,LINQ到数据集,LINQ到XML,LINQ到实体,LINQ Lambda表达式,LINQ与ASP.Net,LINQ查询。

LINQ to XML提供了对所有LINQ功能的轻松访问,例如标准查询运算符,编程接口等.在.NET框架中集成,LINQ to XML还充分利用.NET框架功能,如调试,编译时检查,强大的打字等等.

LINQ to XML简介

使用LINQ to XML时,将XML文档加载到内存中很容易,查询和文档修改更容易.还可以将存储在内存中的XML文档保存到磁盘并对其进行序列化.它使开发人员无需学习有点复杂的XML查询语言.

LINQ to XML在System.Xml.Linq命名空间中具有强大的功能.这包含了使用XML的所有必需的19个类.这些类是以下类.

  • XAttribute

  • XCData

  • XComment

  • XContainer

  • XDeclaration

  • XDocument

  • XDocumentType

  • XElement

  • XName

  • XNamespace

  • XNode

  • XNodeDocumentOrderComparer

  • XNodeEqualityComparer

  • XObject

  • XObjectChange

  • XObjectChangeEventArgs

  • XObjectEventHandler

  • XProcessingInstruction

  • XText

使用LINQ读取XML文件

C#

using System;using System.Collections.Generic;using System.Linq;using System.Xml.Linq;namespace LINQtoXML {   class ExampleOfXML {      static void Main(string[] args) {               string myXML = @"                       Account                       Sales                       Pre-Sales                       Marketing                       ";         XDocument xdoc = new XDocument();         xdoc = XDocument.Parse(myXML);         var result = xdoc.Element("Departments").Descendants();         foreach (XElement item in result) {            Console.WriteLine("Department Name - " + item.Value);         }         Console.WriteLine("\nPress any key to continue.");         Console.ReadKey();      }   } }


VB

Imports System.Collections.GenericImports System.LinqImports System.Xml.LinqModule Module1   Sub Main(ByVal args As String())         Dim myXML As String = "" & vbCr & vbLf &                            "Account" & vbCr & vbLf &                            "Sales" & vbCr & vbLf &                            "Pre-Sales" & vbCr & vbLf &                            "Marketing" & vbCr & vbLf &                            ""      Dim xdoc As New XDocument()      xdoc = XDocument.Parse(myXML)      Dim result = xdoc.Element("Departments").Descendants()      For Each item As XElement In result         Console.WriteLine("Department Name - " + item.Value)      Next      Console.WriteLine(vbLf & "Press any key to continue.")      Console.ReadKey()     End Sub   End Module


当编译并执行上述C#或VB代码时,它会产生以下结果 :

Department Name - AccountDepartment Name - SalesDepartment Name - Pre-SalesDepartment Name - MarketingPress any key to continue.


添加新节点

C#

using System;using System.Collections.Generic;using System.Linq;using System.Xml.Linq;namespace LINQtoXML {   class ExampleOfXML {      static void Main(string[] args) {               string myXML = @"                       Account                       Sales                       Pre-Sales                       Marketing                       ";         XDocument xdoc = new XDocument();         xdoc = XDocument.Parse(myXML);         //Add new Element         xdoc.Element("Departments").Add(new XElement("Department", "Finance"));         //Add new Element at First         xdoc.Element("Departments").AddFirst(new XElement("Department", "Support"));         var result = xdoc.Element("Departments").Descendants();         foreach (XElement item in result) {            Console.WriteLine("Department Name - " + item.Value);         }         Console.WriteLine("\nPress any key to continue.");         Console.ReadKey();      }   } }


VB

Imports System.Collections.GenericImports System.LinqImports System.Xml.LinqModule Module1   Sub Main(ByVal args As String())         Dim myXML As String = "" & vbCr & vbLf &                         "Account" & vbCr & vbLf &                         "Sales" & vbCr & vbLf &                         "Pre-Sales" & vbCr & vbLf &                         "Marketing" & vbCr & vbLf &                         ""      Dim xdoc As New XDocument()      xdoc = XDocument.Parse(myXML)      xdoc.Element("Departments").Add(New XElement("Department", "Finance"))           xdoc.Element("Departments").AddFirst(New XElement("Department", "Support"))      Dim result = xdoc.Element("Departments").Descendants()      For Each item As XElement In result         Console.WriteLine("Department Name - " + item.Value)      Next      Console.WriteLine(vbLf & "Press any key to continue.")      Console.ReadKey()     End Sub   End Module


当编译并执行上述C#或VB代码时,它会产生以下结果 :

Department Name - SupportDepartment Name - AccountDepartment Name - SalesDepartment Name - Pre-SalesDepartment Name - MarketingDepartment Name - FinancePress any key to continue.


删除特定节点

C#

using System;using System.Collections.Generic;using System.Linq;using System.Xml.Linq;namespace LINQtoXML {   class ExampleOfXML {      static void Main(string[] args) {               string myXML = @"                       Support                       Account                       Sales                       Pre-Sales                       Marketing                       Finance                       ";         XDocument xdoc = new XDocument();         xdoc = XDocument.Parse(myXML);         //Remove Sales Department         xdoc.Descendants().Where(s =>s.Value == "Sales").Remove();          var result = xdoc.Element("Departments").Descendants();         foreach (XElement item in result) {            Console.WriteLine("Department Name - " + item.Value);         }         Console.WriteLine("\nPress any key to continue.");         Console.ReadKey();      }   } }


VB

Imports System.Collections.GenericImports System.LinqImports System.Xml.LinqModule Module1   Sub Main(args As String())         Dim myXML As String = "" & vbCr & vbLf &                         "Support" & vbCr & vbLf &                         "Account" & vbCr & vbLf &                         "Sales" & vbCr & vbLf &                         "Pre-Sales" & vbCr & vbLf &                         "Marketing" & vbCr & vbLf &                         "Finance" & vbCr & vbLf &                            ""      Dim xdoc As New XDocument()      xdoc = XDocument.Parse(myXML)           xdoc.Descendants().Where(Function(s) s.Value = "Sales").Remove()      Dim result = xdoc.Element("Departments").Descendants()      For Each item As XElement In result         Console.WriteLine("Department Name - " + item.Value)      Next      Console.WriteLine(vbLf & "Press any key to continue.")      Console.ReadKey()     End Sub   End Module


当上面的C#或VB代码是编译并执行,它产生以下结果 :

Department Name - SupportDepartment Name - AccountDepartment Name - Pre-SalesDepartment Name - MarketingDepartment Name - FinancePress any key to continue.