位置:首頁 > 其他技術 > LinQ教學 > LINQ XML

LINQ XML

LINQ到XML提供易於訪問所有的LINQ功能類似於標準查詢操作,編程接口等。 集成在.NET框架的LINQ to XML也使得物儘其用的.NET框架的功能像調試,編譯時檢查,強類型。

LINQ 到 XML介紹

雖然使用LINQ 到 XML,加載XML文檔到內存容易,更容易被查詢和修改文檔。另外,也可以保存在現有的存儲器磁盤的XML文檔,並對其進行序列化。它省去了一個開發人員學習XML查詢語言這是有點複雜的。

LINQ到XML有它在System.Xml.Linq的命名。這是過去19個必要的類使用XML。這些類如以下所示。

  • 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 = @"<Departments>
                       <Department>Account</Department>
                       <Department>Sales</Department>
                       <Department>Pre-Sales</Department>
                       <Department>Marketing</Department>
                       </Departments>";

        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.Generic
Imports System.Linq
Imports System.Xml.Linq

Module Module1
  Sub Main(ByVal args As String())
     Dim myXML As String = "<Departments>" & vbCr & vbLf & 
                           "<Department>Account</Department>" & vbCr & vbLf & 
                           "<Department>Sales</Department>" & vbCr & vbLf & 
                           "<Department>Pre-Sales</Department>" & vbCr & vbLf & 
                           "<Department>Marketing</Department>" & vbCr & vbLf & "</Departments>"

     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 - Account
Department Name - Sales
Department Name - Pre-Sales
Department Name - Marketing

Press 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 = @"<Departments>
                       <Department>Account</Department>
                       <Department>Sales</Department>
                       <Department>Pre-Sales</Department>
                       <Department>Marketing</Department>
                       </Departments>";

        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.Generic
Imports System.Linq
Imports System.Xml.Linq

Module Module1
  Sub Main(ByVal args As String())
     Dim myXML As String = "<Departments>" & vbCr & vbLf & 
     	                   "<Department>Account</Department>" & vbCr & vbLf & 
     	                   "<Department>Sales</Department>" & vbCr & vbLf & 
     	                   "<Department>Pre-Sales</Department>" & vbCr & vbLf & 
     	                   "<Department>Marketing</Department>" & vbCr & vbLf & 
     	                   "</Departments>"

     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 - Support
Department Name - Account
Department Name - Sales
Department Name - Pre-Sales
Department Name - Marketing
Department Name - Finance

Press 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 = @"<Departments>
                       <Department>Support</Department>
                       <Department>Account</Department>
                       <Department>Sales</Department>
                       <Department>Pre-Sales</Department>
                       <Department>Marketing</Department>
                       <Department>Finance</Department>
                       </Departments>";

        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.Generic
Imports System.Linq
Imports System.Xml.Linq

Module Module1
  Sub Main(args As String())
     Dim myXML As String = "<Departments>" & vbCr & vbLf & 
     	                   "<Department>Support</Department>" & vbCr & vbLf & 
     	                   "<Department>Account</Department>" & vbCr & vbLf & 
     	                   "<Department>Sales</Department>" & vbCr & vbLf & 
     	                   "<Department>Pre-Sales</Department>" & vbCr & vbLf & 
     	                   "<Department>Marketing</Department>" & vbCr & vbLf & 
     	                   "<Department>Finance</Department>" & vbCr & vbLf & "</Departments>"

     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 - Support
Department Name - Account
Department Name - Pre-Sales
Department Name - Marketing
Department Name - Finance

Press any key to continue.