LINQ to XML 總覽LINQ to XML overview

LINQ to XML 提供運用 .NET Language-Integrated Query (LINQ) Framework 的記憶體中 XML 程式開發介面。LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework. LINQ to XML 會使用 .NET 功能,而且相當於更新的、重新設計的文件物件模型 (DOM) XML 程式發展介面。LINQ to XML uses .NET capabilities and is comparable to an updated, redesigned Document Object Model (DOM) XML programming interface.

XML 已被廣泛採用為格式化許多內容之資料的方式。XML has been widely adopted as a way to format data in many contexts. 例如,您可以在 Web、組態檔、Microsoft Office Word 檔案與資料庫中發現 XML。For example, you can find XML on the Web, in configuration files, in Microsoft Office Word files, and in databases.

LINQ to XML 是使用 XML 進行程式設計的最新、重新設計的方法。LINQ to XML is an up-to-date, redesigned approach to programming with XML. 它會提供檔物件模型 (DOM) 的記憶體中檔修改功能,並支援 LINQ 查詢運算式。It provides the in-memory document modification capabilities of the Document Object Model (DOM), and supports LINQ query expressions. 雖然這些查詢運算式在語法上與 XPath 不同,但是它們會提供類似的功能。Although these query expressions are syntactically different from XPath, they provide similar functionality.

LINQ to XML 開發人員LINQ to XML developers

LINQ to XML 以各種開發人員為目標。LINQ to XML targets a variety of developers. 針對只想要完成一些工作的一般開發人員,LINQ to XML 提供類似 SQL 的查詢體驗,讓 XML 更容易。For an average developer who just wants to get something done, LINQ to XML makes XML easier by providing a query experience that's similar to SQL. 只要稍微研究,程式設計人員就可以學會如何以自己選擇的程式語言來撰寫簡潔而且功能強大的查詢。With just a bit of study, programmers can learn to write succinct and powerful queries in their programming language of choice.

專業開發人員可以使用 LINQ to XML 大幅提高生產力。Professional developers can use LINQ to XML to greatly increase their productivity. 藉由 LINQ to XML,他們可以撰寫較少的程式碼,更具表達性、更精簡且更強大。With LINQ to XML, they can write less code that's more expressive, more compact, and more powerful. 他們可以同時使用多個資料網域的查詢運算式。They can use query expressions from multiple data domains at the same time.

LINQ to XML 是 XML 程式設計介面LINQ to XML is an XML programming interface

LINQ to XML 是已啟用 LINQ 的記憶體內部 XML 程式設計介面,可讓您在 .NET 程式設計語言中使用 XML。LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the .NET programming languages.

LINQ to XML 與檔物件模型 (DOM) 一樣,它會將 XML 檔帶入記憶體中。LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory. 您可以查詢與修改文件,並在修改後儲存到檔案,或將其序列化並透過網際網路傳送。You can query and modify the document, and after you modify it you can save it to a file or serialize it and send it over the Internet. 但是,LINQ to XML 與 DOM 不同:However, LINQ to XML differs from DOM:

  • 它提供了較輕量且更容易使用的新物件模型。It provides a new object model that's lighter weight and easier to work with.
  • 它會利用 c # 和 Visual Basic 中的語言功能。It takes advantage of language features in C# and Visual Basic.

LINQ to XML 最重要的優點是它與與語言整合的查詢 (LINQ) 的整合。The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). 這種整合可讓您在記憶體中 XML 文件上撰寫查詢以擷取項目和屬性的集合。This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes. LINQ to XML 的查詢功能可在功能 (中進行比較,不過不像 XPath 和 XQuery 的語法) 。The query capability of LINQ to XML is comparable in functionality (although not in syntax) to XPath and XQuery. C # 與 Visual Basic 的 LINQ 整合提供更強的型別、編譯時間檢查,以及改善的偵錯工具支援。The integration of LINQ in C# and Visual Basic provides stronger typing, compile-time checking, and improved debugger support.

LINQ to XML 的另一個優點是能夠使用查詢結果做為和物件的參數, XElementXAttribute 可讓您建立 XML 樹狀結構的強大方法。Another advantage of LINQ to XML is the ability to use query results as parameters to XElement and XAttribute object constructors enables a powerful approach to creating XML trees. 此方法稱為「功能建構」**,可讓開發人員輕鬆將 XML 樹狀結構從某種圖形轉換為另一種圖形。This approach, called functional construction, enables developers to easily transform XML trees from one shape to another.

例如,您可能會有一般的 XML 採購順序,如 範例 xml 檔:典型採購訂單中所述。For example, you might have a typical XML purchase order as described in Sample XML file: Typical purchase order. 您可以使用 LINQ to XML 來執行下列查詢,以取得訂單中每個專案專案的元件編號屬性值:By using LINQ to XML, you could run the following query to obtain the part number attribute value for every item element in the purchase order:

// Load the XML file from our project directory containing the purchase orders
var filename = "PurchaseOrder.xml";
var currentDirectory = Directory.GetCurrentDirectory();
var purchaseOrderFilepath = Path.Combine(currentDirectory, filename);

XElement purchaseOrder = XElement.Load(purchaseOrderFilepath);

IEnumerable<string> partNos =  from item in purchaseOrder.Descendants("Item")
                               select (string) item.Attribute("PartNumber");
' Load the XML file from our project directory containing the purchase orders
Dim filename = "PurchaseOrder.xml"
Dim currentDirectory = Directory.GetCurrentDirectory()
Dim purchaseOrderFilepath = Path.Combine(currentDirectory, filename)

Dim purchaseOrder As XElement = XElement.Load(purchaseOrderFilepath)

Dim partNos = _
    From item In purchaseOrder...<Item> _
    Select item.@PartNumber

在 c # 中,這可以用方法語法形式重寫:In C# this can be rewritten in method syntax form:

IEnumerable<string> partNos = purchaseOrder.Descendants("Item").Select(x => (string) x.Attribute("PartNumber"));

另一個範例是,您可能會想要一份值大於 $100 之項目的清單 (以零件編號排序)。As another example, you might want a list, sorted by part number, of the items with a value greater than $100. 若要取得此資訊,您可以執行下列查詢:To obtain this information, you could run the following query:

// Load the XML file from our project directory containing the purchase orders
var filename = "PurchaseOrder.xml";
var currentDirectory = Directory.GetCurrentDirectory();
var purchaseOrderFilepath = Path.Combine(currentDirectory, filename);

XElement purchaseOrder = XElement.Load(purchaseOrderFilepath);

IEnumerable<XElement> pricesByPartNos =  from item in purchaseOrder.Descendants("Item")
                                 where (int) item.Element("Quantity") * (decimal) item.Element("USPrice") > 100
                                 orderby (string)item.Element("PartNumber")
                                 select item;
' Load the XML file from our project directory containing the purchase orders
Dim filename = "PurchaseOrder.xml"
Dim currentDirectory = Directory.GetCurrentDirectory()
Dim purchaseOrderFilepath = Path.Combine(currentDirectory, filename)

Dim purchaseOrder As XElement = XElement.Load(purchaseOrderFilepath)

Dim partNos = _
From item In purchaseOrder...<Item> _
Where (item.<Quantity>.Value * _
       item.<USPrice>.Value) > 100 _
Order By item.<PartNumber>.Value _
Select item

同樣地,在 c # 中,這可以用方法語法形式重寫:Again, in C# this can be rewritten in method syntax form:

IEnumerable<XElement> pricesByPartNos = purchaseOrder.Descendants("Item")
                                        .Where(item => (int)item.Element("Quantity") * (decimal)item.Element("USPrice") > 100)
                                        .OrderBy(order => order.Element("PartNumber"));

除了這些 LINQ 功能,LINQ to XML 還提供改良的 XML 程式設計介面。In addition to these LINQ capabilities, LINQ to XML provides an improved XML programming interface. 使用 LINQ to XML,您可以:Using LINQ to XML, you can:

  • 從檔案資料流程載入 XML。Load XML from files or streams.
  • 將 XML 序列化為檔案或資料流。Serialize XML to files or streams.
  • 使用功能結構從頭開始建立 XML。Create XML from scratch by using functional construction.
  • 使用類似 XPath 的座標軸查詢 XML。Query XML using XPath-like axes.
  • 使用 AddRemoveReplaceWithSetValue 之類的方法管理記憶體中 XML 樹狀結構。Manipulate the in-memory XML tree by using methods such as Add, Remove, ReplaceWith, and SetValue.
  • 使用 XSD 驗證 XML 樹狀結構。Validate XML trees using XSD.
  • 使用這些功能的組合,將 XML 樹狀結構從一個組織結構轉換為另一個組織結構。Use a combination of these features to transform XML trees from one shape into another.

建立 XML 樹狀結構Create XML trees

使用 LINQ to XML 進行程式設計最重要的優點之一,就是可以輕鬆地建立 XML 樹狀結構。One of the most significant advantages of programming with LINQ to XML is that it's easy to create XML trees. 例如,若要建立小型 XML 樹狀結構,您可以使用下列方式來撰寫程式碼:For example, to create a small XML tree, you can write code as follows:

XElement contacts =
new XElement("Contacts",
    new XElement("Contact",
        new XElement("Name", "Patrick Hines"),
        new XElement("Phone", "206-555-0144",
            new XAttribute("Type", "Home")),
        new XElement("phone", "425-555-0145",
            new XAttribute("Type", "Work")),
        new XElement("Address",
            new XElement("Street1", "123 Main St"),
            new XElement("City", "Mercer Island"),
            new XElement("State", "WA"),
            new XElement("Postal", "68042")
        )
    )
);
Dim contacts As XElement = _
    <Contacts>
        <Contact>
            <Name>Patrick Hines</Name>
            <Phone>206-555-0144</Phone>
            <Address>
                <Street1>123 Main St</Street1>
                <City>Mercer Island</City>
                <State>WA</State>
                <Postal>68042</Postal>
            </Address>
        </Contact>
    </Contacts>

注意

範例的 Visual Basic 版本會使用 XML 常值。The Visual Basic version of the example uses XML literals. 您也可以 XElement 在 Visual Basic 中使用,如同 c # 版本。You can also use XElement in Visual Basic, as in the C# version.

如需詳細資訊,請參閱 XML 樹狀結構。For more information, see XML trees.

另請參閱See also