LINQ to XML 比較其他 XML 技術之比較LINQ to XML vs. Other XML Technologies

這個主題會比較 LINQ to XMLLINQ to XML 與下列 XML 技術:XmlReader、XSLT、MSXML 和 XmlLite。This topic compares LINQ to XMLLINQ to XML to the following XML technologies: XmlReader, XSLT, MSXML, and XmlLite. 這個資訊可以協助您決定要使用的技術。This information can help you decide which technology to use.

若要比較 LINQ to XMLLINQ to XML 與文件物件模型 (DOM),請參閱 LINQ to XML 與DOM (C#) 之比較For a comparison of LINQ to XMLLINQ to XML to the Document Object Model (DOM), see LINQ to XML vs. DOM (C#).

LINQ to XML 比較XmlReaderLINQ to XML vs. XmlReader

XmlReader 是一個快速、順向、非快取的剖析器。XmlReader is a fast, forward-only, non-caching parser.

LINQ to XMLLINQ to XML 會在 XmlReader 頂端實作,而且它們會緊密整合。is implemented on top of XmlReader, and they are tightly integrated. 不過,您也可以單獨使用 XmlReaderHowever, you can also use XmlReader by itself.

例如,假設您要建置每秒將會剖析數百個 XML 文件的 Web 服務,而且這些文件的結構相同,表示您只需要撰寫一個程式碼實作,就可以剖析 XML。For example, suppose you are building a Web service that will parse hundreds of XML documents per second, and the documents have the same structure, meaning that you only have to write one implementation of the code to parse the XML. 在這個情況下,您可能想要單獨使用 XmlReaderIn this case, you would probably want to use XmlReader by itself.

相較之下,如果您要建置剖析許多較小 XML 文件的系統,而且每個文件都不同,您可能就會想要利用 LINQ to XMLLINQ to XML 所提供的生產力提升功能。In contrast, if you are building a system that parses many smaller XML documents, and each one is different, you would want to take advantage of the productivity improvements that LINQ to XMLLINQ to XML provides.

LINQ to XML 比較XSLTLINQ to XML vs. XSLT

LINQ to XMLLINQ to XML 和 XSLT 都提供大量的 XML 文件轉換功能。Both LINQ to XMLLINQ to XML and XSLT provide extensive XML document transformation capabilities. XSLT 是一種以規則為基礎的宣告式方法。XSLT is a rule-based, declarative approach. 高級的 XSLT 程式設計人員會以功能性程式設計方式,撰寫強調沒有狀態 (Stateless) 方法的 XSLT。Advanced XSLT programmers write XSLT in a functional programming style that emphasizes a stateless approach. 您可以使用實作時沒有副作用的純虛擬函式來撰寫轉換。Transformations can be written using pure functions that are implemented without side effects. 許多開發人員不熟悉這個以規則為基礎或功能性方法,而且學習起來可能既困難又耗時。This rule-based or functional approach is unfamiliar to many developers, and can be difficult and time-consuming to learn.

XSLT 可以是非常有產能的系統,可產生高效能的應用程式。XSLT can be a very productive system that yields high-performance applications. 例如,有些大型的網路公司會使用 XSLT,根據已經從各種資料存放區提取的 XML 產生 HTML。For example, some big Web companies use XSLT as a way to generate HTML from XML that has been pulled from a variety of data stores. Managed XSLT 引擎會將 XSLT 編譯為 CLR 程式碼,而且在某些案例中的效能,甚至比原始 XSLT 引擎更好。The managed XSLT engine compiles XSLT to CLR code, and performs even better in some scenarios than the native XSLT engine.

不過,XSLT 不會使用許多開發人員已經具備的 C# 和 Visual Basic 知識。However, XSLT does not take advantage of the C# and Visual Basic knowledge that many developers have. 它需要開發人員以不同而且複雜的程式設計語言撰寫程式碼。It requires developers to write code in a different and complex programming language. 使用兩個非整合式開發系統 (例如,C# (或 Visual Basic) 和 XSLT) 會使軟體系統難以開發與維護。Using two non-integrated development systems such as C# (or Visual Basic) and XSLT results in software systems that are more difficult to develop and maintain.

在您熟練 LINQ to XMLLINQ to XML 查詢運算式後,LINQ to XMLLINQ to XML 轉換即可成為功能強大且易於使用的技術。After you have mastered LINQ to XMLLINQ to XML query expressions, LINQ to XMLLINQ to XML transformations are a powerful technology that is easy to use. 基本上,您可以形成自己的 XML 文件,方法是,使用功能結構、從各種來源納入資料、動態建構 XElement 物件,然後將全部組合成一個新的 XML 樹狀結構。Basically, you form your XML document by using functional construction, pulling in data from various sources, constructing XElement objects dynamically, and assembling the whole into a new XML tree. 轉換可以產生全新的文件。The transformation can generate a completely new document. LINQ to XMLLINQ to XML 中進行建構轉換是相當容易與直覺化的作業,並會產生可讀的程式碼。Constructing transformations in LINQ to XMLLINQ to XML is relatively easy and intuitive, and the resulting code is readable. 這會降低開發與維護的成本。This reduces development and maintenance costs.

LINQ to XMLLINQ to XML 並不是要取代 XSLT。is not intended to replace XSLT. XSLT 仍然是複雜的文件中心 XML 轉換的最佳工具選擇,特別是在沒有正確定義文件結構時。XSLT is still the tool of choice for complicated and document-centric XML transformations, especially if the structure of the document is not well defined.

XSLT 具有做為全球資訊網協會 (W3C) 標準的優點。XSLT has the advantage of being a World Wide Web Consortium (W3C) standard. 如果您的需求為只使用標準的技術,XSLT 可能更合適。If you have a requirement that you use only technologies that are standards, XSLT might be more appropriate.

XSLT 是 XML,因此可以用程式設計方式操作。XSLT is XML, and therefore can be programmatically manipulated.

LINQ to XML 比較MSXMLLINQ to XML vs. MSXML

MSXML 是 COM 架構的技術,用於處理 Microsoft Windows 隨附的 XML。MSXML is the COM-based technology for processing XML that is included with Microsoft Windows. MSXML 會提供 DOM 的原始實作 (包含對於 XPath 和 XSLT 的支援),MSXML provides a native implementation of the DOM with support for XPath and XSLT. 同時也包含 SAX2 非快取的事件型剖析器。It also contains the SAX2 non-caching, event-based parser.

MSXML 運作良好、在大部分的案例中預設是安全的,而且可以在 Internet Explorer 中存取,以便在 AJAX 型的應用程式中執行用戶端的 XML 處理。MSXML performs well, is secure by default in most scenarios, and can be accessed in Internet Explorer for performing client-side XML processing in AJAX-style applications. 從支援 COM (包括 C++、JavaScript 和 Visual Basic 6.0) 的任何程式設計語言都可以使用 MSXML。MSXML can be used from any programming language that supports COM, including C++, JavaScript, and Visual Basic 6.0.

根據 Common Language Runtime (CLR),不建議將 MSXML 用於 Managed 程式碼中。MSXML is not recommended for use in managed code based on the common language runtime (CLR).

LINQ to XML 比較XmlLiteLINQ to XML vs. XmlLite

XmlLite 是一個非快取、順向、提取的剖析器。XmlLite is a non-caching, forward only, pull parser. 開發人員主要會使用 XmlLite 搭配 C++。Developers primarily use XmlLite with C++. 不建議開發人員使用 XmlLite 搭配 Managed 程式碼。It is not recommended for developers to use XmlLite with managed code.

XmlLite 的主要優點在於它是一個快速的輕量型 XML 剖析器,在大部分的案例中是安全的。The main advantage of XmlLite is that it is a lightweight, fast XML parser that is secure in most scenarios. 其威脅表面區域非常小。Its threat surface area is very small. 如果您必須剖析不受信任的文件,而且您想要防止諸如阻絕服務或洩漏資料等攻擊,XmlLite 可能是一個相當好的選擇。If you have to parse untrusted documents and you want to protect against attacks such as denial of service or exposure of data, XmlLite might be a good option.

XmlLite 不會與 Language-Integrated Query (LINQ)Language-Integrated Query (LINQ) 整合在一起。XmlLite is not integrated with Language-Integrated Query (LINQ)Language-Integrated Query (LINQ). 因此不會提供程式設計人員生產力提升功能,這種功能是 LINQLINQ 背後的驅動力量。It does not yield the programmer productivity improvements that are the motivating force behind LINQLINQ.

另請參閱See also