diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index bf284a8d762e8..9dfe2fa353533 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -1209,6 +1209,16 @@ "redirect_url": "/dotnet/standard/linq/linq-xml-overview", "redirect_document_id": true }, + { + "source_path": "docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-dom.md", + "redirect_url": "/dotnet/standard/linq/linq-xml-vs-dom", + "redirect_document_id": true + }, + { + "source_path": "docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md", + "redirect_url": "/dotnet/standard/linq/linq-xml-vs-xml-technologies", + "redirect_document_id": true + }, { "source_path": "docs/csharp/programming-guide/concepts/threading/how-to-use-a-thread-pool.md", "redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem" @@ -4172,6 +4182,14 @@ "source_path": "docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-overview.md", "redirect_url": "/dotnet/standard/linq/linq-xml-overview" }, + { + "source_path": "docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-dom.md", + "redirect_url": "/dotnet/standard/linq/linq-xml-vs-dom" + }, + { + "source_path": "docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md", + "redirect_url": "/dotnet/standard/linq/linq-xml-vs-xml-technologies" + }, { "source_path": "docs/visual-basic/programming-guide/concepts/threading/how-to-use-a-thread-pool.md", "redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem" diff --git a/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-dom.md b/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-dom.md deleted file mode 100644 index 3410c2eef2340..0000000000000 --- a/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-dom.md +++ /dev/null @@ -1,130 +0,0 @@ ---- -title: "LINQ to XML vs. DOM (C#)" -ms.date: 07/20/2015 -ms.assetid: 51c0e3d2-c047-4e6a-a423-d61a882400b7 ---- -# LINQ to XML vs. DOM (C#) -This section describes some key differences between [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] and the current predominant XML programming API, the W3C Document Object Model (DOM). - -## New Ways to Construct XML Trees - In the W3C DOM, you build an XML tree from the bottom up; that is, you create a document, you create elements, and then you add the elements to the document. - - For example, the following would be a typical way to create an XML tree using the Microsoft implementation of DOM, : - -```csharp -XmlDocument doc = new XmlDocument(); -XmlElement name = doc.CreateElement("Name"); -name.InnerText = "Patrick Hines"; -XmlElement phone1 = doc.CreateElement("Phone"); -phone1.SetAttribute("Type", "Home"); -phone1.InnerText = "206-555-0144"; -XmlElement phone2 = doc.CreateElement("Phone"); -phone2.SetAttribute("Type", "Work"); -phone2.InnerText = "425-555-0145"; -XmlElement street1 = doc.CreateElement("Street1"); -street1.InnerText = "123 Main St"; -XmlElement city = doc.CreateElement("City"); -city.InnerText = "Mercer Island"; -XmlElement state = doc.CreateElement("State"); -state.InnerText = "WA"; -XmlElement postal = doc.CreateElement("Postal"); -postal.InnerText = "68042"; -XmlElement address = doc.CreateElement("Address"); -address.AppendChild(street1); -address.AppendChild(city); -address.AppendChild(state); -address.AppendChild(postal); -XmlElement contact = doc.CreateElement("Contact"); -contact.AppendChild(name); -contact.AppendChild(phone1); -contact.AppendChild(phone2); -contact.AppendChild(address); -XmlElement contacts = doc.CreateElement("Contacts"); -contacts.AppendChild(contact); -doc.AppendChild(contacts); -``` - - This style of coding does not visually provide much information about the structure of the XML tree. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] supports this approach to constructing an XML tree, but also supports an alternative approach, *functional construction*. Functional construction uses the and constructors to build an XML tree. - - Here is how you would construct the same XML tree by using [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] functional construction: - -```csharp -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") - ) - ) - ); -``` - - Notice that indenting the code to construct the XML tree shows the structure of the underlying XML. - - For more information, see [Creating XML Trees (C#)](./linq-to-xml-overview.md). - -## Working Directly with XML Elements - When you program with XML, your primary focus is usually on XML elements and perhaps on attributes. In [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)], you can work directly with XML elements and attributes. For example, you can do the following: - -- Create XML elements without using a document object at all. This simplifies programming when you have to work with fragments of XML trees. - -- Load `T:System.Xml.Linq.XElement` objects directly from an XML file. - -- Serialize `T:System.Xml.Linq.XElement` objects to a file or a stream. - - Compare this to the W3C DOM, in which the XML document is used as a logical container for the XML tree. In DOM, XML nodes, including elements and attributes, must be created in the context of an XML document. Here is a fragment of the code to create a name element in DOM: - -```csharp -XmlDocument doc = new XmlDocument(); -XmlElement name = doc.CreateElement("Name"); -name.InnerText = "Patrick Hines"; -doc.AppendChild(name); -``` - - If you want to use an element across multiple documents, you must import the nodes across documents. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] avoids this layer of complexity. - - When using LINQ to XML, you use the class only if you want to add a comment or processing instruction at the root level of the document. - -## Simplified Handling of Names and Namespaces - Handling names, namespaces, and namespace prefixes is generally a complex part of XML programming. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] simplifies names and namespaces by eliminating the requirement to deal with namespace prefixes. If you want to control namespace prefixes, you can. But if you decide to not explicitly control namespace prefixes, [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] will assign namespace prefixes during serialization if they are required, or will serialize using default namespaces if they are not. If default namespaces are used, there will be no namespace prefixes in the resulting document. For more information, see [Namespaces Overview (LINQ to XML) (C#)](namespaces-overview-linq-to-xml.md). - - Another problem with the DOM is that it does not let you change the name of a node. Instead, you have to create a new node and copy all the child nodes to it, losing the original node identity. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] avoids this problem by enabling you to set the property on a node. - -## Static Method Support for Loading XML - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see [How to load XML from a file (C#)](./how-to-load-xml-from-a-file.md). - -## Removal of Support for DTD Constructs - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] further simplifies XML programming by removing support for entities and entity references. The management of entities is complex, and is rarely used. Removing their support increases performance and simplifies the programming interface. When a [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] tree is populated, all DTD entities are expanded. - -## Support for Fragments - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] does not provide an equivalent for the `XmlDocumentFragment` class. In many cases, however, the `XmlDocumentFragment` concept can be handled by the result of a query that is typed as of , or of . - -## Support for XPathNavigator - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides support for through extension methods in the namespace. For more information, see . - -## Support for White Space and Indentation - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] handles white space more simply than the DOM. - - A common scenario is to read indented XML, create an in-memory XML tree without any white space text nodes (that is, not preserving white space), perform some operations on the XML, and then save the XML with indentation. When you serialize the XML with formatting, only significant white space in the XML tree is preserved. This is the default behavior for [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)]. - - Another common scenario is to read and modify XML that has already been intentionally indented. You might not want to change this indentation in any way. In [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)], you can do this by preserving white space when you load or parse the XML and disabling formatting when you serialize the XML. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] stores white space as an node, instead of having a specialized node type, as the DOM does. - -## Support for Annotations - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] elements support an extensible set of annotations. This is useful for tracking miscellaneous information about an element, such as schema information, information about whether the element is bound to a UI, or any other kind of application-specific information. For more information, see [LINQ to XML Annotations](./linq-to-xml-annotations.md). - -## Support for Schema Information -[!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides support for XSD validation through extension methods in the namespace. You can validate that an XML tree complies with an XSD. You can populate the XML tree with the post-schema-validation infoset (PSVI). For more information, see [How to validate using XSD](./how-to-validate-using-xsd-linq-to-xml.md) and . - -## See also - -- [Getting Started (LINQ to XML)](./linq-to-xml-overview.md) diff --git a/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md b/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md deleted file mode 100644 index 2e528205ee7f4..0000000000000 --- a/docs/csharp/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -title: "LINQ to XML vs. Other XML Technologies3" -ms.date: 07/20/2015 -ms.assetid: 01b8e746-12d3-471d-b811-7539e4547784 ---- -# LINQ to XML vs. Other XML Technologies -This topic compares [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] to the following XML technologies: , XSLT, MSXML, and XmlLite. This information can help you decide which technology to use. - - For a comparison of [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] to the Document Object Model (DOM), see [LINQ to XML vs. DOM (C#)](./linq-to-xml-vs-dom.md). - -## LINQ to XML vs. XmlReader - is a fast, forward-only, non-caching parser. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is implemented on top of , and they are tightly integrated. However, you can also use by itself. - - 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. In this case, you would probably want to use by itself. - - 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 [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides. - -## LINQ to XML vs. XSLT - Both [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] and XSLT provide extensive XML document transformation capabilities. XSLT is a rule-based, declarative approach. 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 can be a very productive system that yields high-performance applications. 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. The managed XSLT engine compiles XSLT to CLR code, and performs even better in some scenarios than the native XSLT engine. - - 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. 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. - - After you have mastered [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] query expressions, [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] transformations are a powerful technology that is easy to use. Basically, you form your XML document by using functional construction, pulling in data from various sources, constructing objects dynamically, and assembling the whole into a new XML tree. The transformation can generate a completely new document. Constructing transformations in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is relatively easy and intuitive, and the resulting code is readable. This reduces development and maintenance costs. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is not intended to replace XSLT. 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 has the advantage of being a World Wide Web Consortium (W3C) standard. If you have a requirement that you use only technologies that are standards, XSLT might be more appropriate. - - XSLT is XML, and therefore can be programmatically manipulated. - -## LINQ to XML vs. MSXML - MSXML is the COM-based technology for processing XML that is included with Microsoft Windows. MSXML provides a native implementation of the DOM with support for XPath and XSLT. It also contains the SAX2 non-caching, event-based parser. - - 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. MSXML can be used from any programming language that supports COM, including C++, JavaScript, and Visual Basic 6.0. - - MSXML is not recommended for use in managed code based on the common language runtime (CLR). - -## LINQ to XML vs. XmlLite - XmlLite is a non-caching, forward only, pull parser. Developers primarily use XmlLite with C++. It is not recommended for developers to use XmlLite with managed code. - - 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. 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 is not integrated with Language-Integrated Query (LINQ). It does not yield the programmer productivity improvements that are the motivating force behind LINQ. - -## See also - -- [Getting Started (LINQ to XML)](./linq-to-xml-overview.md) diff --git a/docs/standard/linq/linq-xml-vs-dom.md b/docs/standard/linq/linq-xml-vs-dom.md new file mode 100644 index 0000000000000..6289ee6fe0a2c --- /dev/null +++ b/docs/standard/linq/linq-xml-vs-dom.md @@ -0,0 +1,202 @@ +--- +title: "LINQ to XML vs. DOM" +ms.date: 07/20/2015 +dev_langs: + - "csharp" + - "vb" +ms.assetid: 51c0e3d2-c047-4e6a-a423-d61a882400b7 +--- + +# LINQ to XML vs. DOM + +This article describes some key differences between LINQ to XML and the current predominant XML programming API, the W3C Document Object Model (DOM). + +## New ways to construct XML trees + +In the W3C DOM, you build an XML tree from the bottom up; that is, you create a document, you create elements, and then you add the elements to the document. + +For example, the following example uses a typical way to create an XML tree using the Microsoft implementation of DOM, . + +```csharp +XmlDocument doc = new XmlDocument(); +XmlElement name = doc.CreateElement("Name"); +name.InnerText = "Patrick Hines"; +XmlElement phone1 = doc.CreateElement("Phone"); +phone1.SetAttribute("Type", "Home"); +phone1.InnerText = "206-555-0144"; +XmlElement phone2 = doc.CreateElement("Phone"); +phone2.SetAttribute("Type", "Work"); +phone2.InnerText = "425-555-0145"; +XmlElement street1 = doc.CreateElement("Street1"); +street1.InnerText = "123 Main St"; +XmlElement city = doc.CreateElement("City"); +city.InnerText = "Mercer Island"; +XmlElement state = doc.CreateElement("State"); +state.InnerText = "WA"; +XmlElement postal = doc.CreateElement("Postal"); +postal.InnerText = "68042"; +XmlElement address = doc.CreateElement("Address"); +address.AppendChild(street1); +address.AppendChild(city); +address.AppendChild(state); +address.AppendChild(postal); +XmlElement contact = doc.CreateElement("Contact"); +contact.AppendChild(name); +contact.AppendChild(phone1); +contact.AppendChild(phone2); +contact.AppendChild(address); +XmlElement contacts = doc.CreateElement("Contacts"); +contacts.AppendChild(contact); +doc.AppendChild(contacts); +``` + +```vb +Dim doc As XmlDocument = New XmlDocument() +Dim name As XmlElement = doc.CreateElement("Name") +name.InnerText = "Patrick Hines" +Dim phone1 As XmlElement = doc.CreateElement("Phone") +phone1.SetAttribute("Type", "Home") +phone1.InnerText = "206-555-0144" +Dim phone2 As XmlElement = doc.CreateElement("Phone") +phone2.SetAttribute("Type", "Work") +phone2.InnerText = "425-555-0145" +Dim street1 As XmlElement = doc.CreateElement("Street1") +street1.InnerText = "123 Main St" +Dim city As XmlElement = doc.CreateElement("City") +city.InnerText = "Mercer Island" +Dim state As XmlElement = doc.CreateElement("State") +state.InnerText = "WA" +Dim postal As XmlElement = doc.CreateElement("Postal") +postal.InnerText = "68042" +Dim address As XmlElement = doc.CreateElement("Address") +address.AppendChild(street1) +address.AppendChild(city) +address.AppendChild(state) +address.AppendChild(postal) +Dim contact As XmlElement = doc.CreateElement("Contact") +contact.AppendChild(name) +contact.AppendChild(phone1) +contact.AppendChild(phone2) +contact.AppendChild(address) +Dim contacts As XmlElement = doc.CreateElement("Contacts") +contacts.AppendChild(contact) +doc.AppendChild(contacts) +Console.WriteLine(doc.OuterXml) +``` + +This style of coding hides the structure of the XML tree. LINQ to XML also supports an alternative approach, *functional construction*, that better shows the structure. This approach can be done with the and constructors. In Visual Basic, it can also be done with XML literals. This example demonstrates construction of the same XML tree using functional construction: + +```csharp +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") + ) + ) + ); +``` + +```vb +Dim contacts = _ + + + Patrick Hines + 206-555-0144 + 425-555-0145 +
+ 123 Main St + Mercer Island + WA + 68042 +
+
+
+``` + +Notice that indenting the code to construct the XML tree shows the structure of the underlying XML. The Visual Basic version uses XML literals. + +For more information, see [XML trees](functional-construction.md). + +## Work directly with XML elements + +When you program with XML, your primary focus is usually on XML elements and perhaps on attributes. In LINQ to XML, you can work directly with XML elements and attributes. For example, you can do the following: + +- Create XML elements without using a document object at all. This simplifies programming when you have to work with fragments of XML trees. +- Load `T:System.Xml.Linq.XElement` objects directly from an XML file. +- Serialize `T:System.Xml.Linq.XElement` objects to a file or a stream. + +Compare this to the W3C DOM, in which the XML document is used as a logical container for the XML tree. In DOM, XML nodes, including elements and attributes, must be created in the context of an XML document. Here is a fragment of code to create a name element in DOM: + +```csharp +XmlDocument doc = new XmlDocument(); +XmlElement name = doc.CreateElement("Name"); +name.InnerText = "Patrick Hines"; +doc.AppendChild(name); +``` + +```vb +Dim doc As XmlDocument = New XmlDocument() +Dim name As XmlElement = doc.CreateElement("Name") +name.InnerText = "Patrick Hines" +doc.AppendChild(name) +``` + +If you want to use an element across multiple documents, you must import the nodes across documents. LINQ to XML avoids this layer of complexity. + +When using LINQ to XML, you use the class only if you want to add a comment or processing instruction at the root level of the document. + +## Simplified handling of names and namespaces + +Handling names, namespaces, and namespace prefixes is generally a complex part of XML programming. LINQ to XML simplifies names and namespaces by eliminating the requirement to deal with namespace prefixes. If you want to control namespace prefixes, you can. But if you decide to not explicitly control namespace prefixes, LINQ to XML will assign namespace prefixes during serialization if they're required, or will serialize using default namespaces if they aren't. If default namespaces are used, there will be no namespace prefixes in the resulting document. For more information, see [Namespaces overview (LINQ to XML)](namespaces-overview.md). + +Another problem with the DOM is that it doesn't let you change the name of a node. Instead, you have to create a new node and copy all the child nodes to it, losing the original node identity. LINQ to XML avoids this problem by enabling you to set the property on a node. + +## Static method support for loading XML + +LINQ to XML lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see [How to: Load XML from a file](load-xml-file.md). + +## Removal of support for DTD constructs + +LINQ to XML further simplifies XML programming by removing support for entities and entity references. The management of entities is complex, and is rarely used. Removing their support increases performance and simplifies the programming interface. When a LINQ to XML tree is populated, all DTD entities are expanded. + +## Support for fragments + +LINQ to XML doesn't provide an equivalent for the `XmlDocumentFragment` class. In many cases, however, the `XmlDocumentFragment` concept can be handled by the result of a query that is typed as of , or of . + +## Support for XPathNavigator + +LINQ to XML provides support for through extension methods in the namespace. For more information, see . + +## Support for white space and indentation + +LINQ to XML handles white space more simply than the DOM. + +A common scenario is to read indented XML, create an in-memory XML tree without any white-space text nodes (that is, not preserving white space), do some operations on the XML, and then save the XML with indentation. When you serialize the XML with formatting, only significant white space in the XML tree is preserved. This is the default behavior for LINQ to XML. + +Another common scenario is to read and modify XML that has already been intentionally indented. You might not want to change this indentation in any way. In LINQ to XML, you can do this by: + +- Preserving white space when you load or parse the XML. +- Disabling formatting when you serialize the XML. + +LINQ to XML stores white space as an node, instead of having a specialized node type, as the DOM does. + +## Support for annotations + +LINQ to XML elements support an extensible set of annotations. This is useful for tracking miscellaneous information about an element, such as schema information, information about whether the element is bound to a UI, or any other kind of application-specific information. For more information, see [LINQ to XML annotations](linq-xml-annotations.md). + +## Support for schema information + +LINQ to XML provides support for XSD validation through extension methods in the namespace. You can validate that an XML tree complies with an XSD. You can populate the XML tree with the post-schema-validation infoset (PSVI). For more information, see [How to: Validate using XSD](validate-xsd.md) and . + +## See also + +- [LINQ to XML overview](linq-xml-overview.md) diff --git a/docs/standard/linq/linq-xml-vs-xml-technologies.md b/docs/standard/linq/linq-xml-vs-xml-technologies.md new file mode 100644 index 0000000000000..098147ced8631 --- /dev/null +++ b/docs/standard/linq/linq-xml-vs-xml-technologies.md @@ -0,0 +1,57 @@ +--- +title: "LINQ to XML vs. other XML technologies" +ms.date: 07/20/2015 +ms.assetid: 01b8e746-12d3-471d-b811-7539e4547784 +--- + +# LINQ to XML vs. other XML technologies + +This article compares LINQ to XML to the following XML technologies: , XSLT, MSXML, and XmlLite. This information can help you decide which technology to use. + +For a comparison of LINQ to XML to the Document Object Model (DOM), see [LINQ to XML vs. DOM](linq-xml-vs-dom.md). + +## LINQ to XML vs. XmlReader + + is a fast, forward-only, non-caching parser. + +LINQ to XML is implemented on top of , and they're tightly integrated. However, you can also use directly. + +For example, suppose you're 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. In this case, you would probably want to use directly. + +In contrast, if you're building a system that parses many smaller XML documents, and each one is different, you'd want to take advantage of the productivity improvements that LINQ to XML provides. + +## LINQ to XML vs. XSLT + +Both LINQ to XML and XSLT provide extensive XML document transformation capabilities. XSLT is a rule-based, declarative approach. 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 can be a productive system that yields high-performance applications. For example, some large Web companies use XSLT as a way to generate HTML from XML that has been pulled from different kinds of data stores. The managed XSLT engine compiles XSLT to common language runtime (CLR) code, and performs even better in some scenarios than the native XSLT engine. + +However, XSLT doesn't 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. 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. + +After you have mastered LINQ to XML query expressions, LINQ to XML transformations are a powerful technology that is easy to use. Basically, you form your XML document by using functional construction, pulling in data from various sources, constructing objects dynamically, and assembling the whole into a new XML tree. The transformation can generate a completely new document. Constructing transformations in LINQ to XML is relatively easy and intuitive, and the resulting code is readable. This reduces development and maintenance costs. + +LINQ to XML isn't intended to replace XSLT. XSLT is still the tool of choice for complicated and document-centric XML transformations, especially if the structure of the document isn't well defined. + +XSLT has the advantage of being a World Wide Web Consortium (W3C) standard. If you have a requirement that you use only technologies that are standards, XSLT might be more appropriate. + +XSLT is XML, and that's why it can be programmatically manipulated. + +## LINQ to XML vs. MSXML + +MSXML is the COM-based technology for processing XML that is included with Microsoft Windows. MSXML provides a native implementation of the DOM with support for XPath and XSLT. It also contains the SAX2 non-caching, event-based parser. + +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. MSXML can be used from any programming language that supports COM, including C++, JavaScript, and Visual Basic 6.0. + +MSXML isn't recommended for use in managed code based on the CLR. + +## LINQ to XML vs. XmlLite + +XmlLite is a non-caching, forward only, pull parser. Developers primarily use XmlLite with C++. It isn't recommended for developers to use XmlLite with managed code. + +The main advantage of XmlLite is that it's a lightweight, fast XML parser that is secure in most scenarios. Its threat surface area is very small. 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 isn't integrated with Language-Integrated Query (LINQ). It doesn't yield the programmer productivity improvements that are the motivating force behind LINQ. + +## See also + +- [Linq to XML overview](linq-xml-overview.md) diff --git a/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-dom.md b/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-dom.md deleted file mode 100644 index 715e9b586db83..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-dom.md +++ /dev/null @@ -1,129 +0,0 @@ ---- -title: "LINQ to XML vs. DOM" -ms.date: 07/20/2015 -ms.assetid: 18c36130-d598-40b7-9007-828232252978 ---- -# LINQ to XML vs. DOM (Visual Basic) -This section describes some key differences between [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] and the current predominant XML programming API, the W3C Document Object Model (DOM). - -## New Ways to Construct XML Trees - In the W3C DOM, you build an XML tree from the bottom up; that is, you create a document, you create elements, and then you add the elements to the document. - - For example, the following would be a typical way to create an XML tree using the Microsoft implementation of DOM, : - -```vb -Dim doc As XmlDocument = New XmlDocument() -Dim name As XmlElement = doc.CreateElement("Name") -name.InnerText = "Patrick Hines" -Dim phone1 As XmlElement = doc.CreateElement("Phone") -phone1.SetAttribute("Type", "Home") -phone1.InnerText = "206-555-0144" -Dim phone2 As XmlElement = doc.CreateElement("Phone") -phone2.SetAttribute("Type", "Work") -phone2.InnerText = "425-555-0145" -Dim street1 As XmlElement = doc.CreateElement("Street1") -street1.InnerText = "123 Main St" -Dim city As XmlElement = doc.CreateElement("City") -city.InnerText = "Mercer Island" -Dim state As XmlElement = doc.CreateElement("State") -state.InnerText = "WA" -Dim postal As XmlElement = doc.CreateElement("Postal") -postal.InnerText = "68042" -Dim address As XmlElement = doc.CreateElement("Address") -address.AppendChild(street1) -address.AppendChild(city) -address.AppendChild(state) -address.AppendChild(postal) -Dim contact As XmlElement = doc.CreateElement("Contact") -contact.AppendChild(name) -contact.AppendChild(phone1) -contact.AppendChild(phone2) -contact.AppendChild(address) -Dim contacts As XmlElement = doc.CreateElement("Contacts") -contacts.AppendChild(contact) -doc.AppendChild(contacts) -Console.WriteLine(doc.OuterXml) -``` - - This style of coding does not visually provide much information about the structure of the XML tree. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] supports this approach to constructing an XML tree, but also supports an alternative approach, *functional construction*. In Visual Basic, functional construction uses XML literals to build an XML tree. - - Here is how you would construct the same XML tree by using [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] functional construction: - -```vb -Dim contacts = _ - - - Patrick Hines - 206-555-0144 - 425-555-0145 -
- 123 Main St - Mercer Island - WA - 68042 -
-
-
-``` - - Notice that indenting the code to construct the XML tree shows the structure of the underlying XML. - - For more information, see [Creating XML Trees (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/creating-xml-trees.md). - -## Working Directly with XML Elements - When you program with XML, your primary focus is usually on XML elements and perhaps on attributes. In [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)], you can work directly with XML elements and attributes. For example, you can do the following: - -- Create XML elements without using a document object at all. This simplifies programming when you have to work with fragments of XML trees. - -- Load `T:System.Xml.Linq.XElement` objects directly from an XML file. - -- Serialize `T:System.Xml.Linq.XElement` objects to a file or a stream. - - Compare this to the W3C DOM, in which the XML document is used as a logical container for the XML tree. In DOM, XML nodes, including elements and attributes, must be created in the context of an XML document. Here is a fragment of the code to create a name element in DOM: - -```vb -Dim doc As XmlDocument = New XmlDocument() -Dim name As XmlElement = doc.CreateElement("Name") -name.InnerText = "Patrick Hines" -doc.AppendChild(name) -``` - - If you want to use an element across multiple documents, you must import the nodes across documents. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] avoids this layer of complexity. - - When using LINQ to XML, you use the class only if you want to add a comment or processing instruction at the root level of the document. - -## Simplified Handling of Names and Namespaces - Handling names, namespaces, and namespace prefixes is generally a complex part of XML programming. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] simplifies names and namespaces by eliminating the requirement to deal with namespace prefixes. If you want to control namespace prefixes, you can. But if you decide to not explicitly control namespace prefixes, [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] will assign namespace prefixes during serialization if they are required, or will serialize using default namespaces if they are not. If default namespaces are used, there will be no namespace prefixes in the resulting document. For more information, see [Namespaces Overview (LINQ to XML) (Visual Basic)](namespaces-overview-linq-to-xml.md). - - Another problem with the DOM is that it does not let you change the name of a node. Instead, you have to create a new node and copy all the child nodes to it, losing the original node identity. [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] avoids this problem by enabling you to set the property on a node. - -## Static Method Support for Loading XML - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] lets you load XML by using static methods, instead of instance methods. This simplifies loading and parsing. For more information, see [How to: Load XML from a File (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/how-to-load-xml-from-a-file.md). - -## Removal of Support for DTD Constructs - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] further simplifies XML programming by removing support for entities and entity references. The management of entities is complex, and is rarely used. Removing their support increases performance and simplifies the programming interface. When a [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] tree is populated, all DTD entities are expanded. - -## Support for Fragments - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] does not provide an equivalent for the `XmlDocumentFragment` class. In many cases, however, the `XmlDocumentFragment` concept can be handled by the result of a query that is typed as of , or of . - -## Support for XPathNavigator - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides support for through extension methods in the namespace. For more information, see . - -## Support for White Space and Indentation - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] handles white space more simply than the DOM. - - A common scenario is to read indented XML, create an in-memory XML tree without any white space text nodes (that is, not preserving white space), perform some operations on the XML, and then save the XML with indentation. When you serialize the XML with formatting, only significant white space in the XML tree is preserved. This is the default behavior for [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)]. - - Another common scenario is to read and modify XML that has already been intentionally indented. You might not want to change this indentation in any way. In [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)], you can do this by preserving white space when you load or parse the XML and disabling formatting when you serialize the XML. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] stores white space as an node, instead of having a specialized node type, as the DOM does. - -## Support for Annotations - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] elements support an extensible set of annotations. This is useful for tracking miscellaneous information about an element, such as schema information, information about whether the element is bound to a UI, or any other kind of application-specific information. For more information, see [LINQ to XML Annotations](../../../../visual-basic/programming-guide/concepts/linq/linq-to-xml-annotations.md). - -## Support for Schema Information - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides support for XSD validation through extension methods in the namespace. You can validate that an XML tree complies with an XSD. You can populate the XML tree with the post-schema-validation infoset (PSVI). For more information, see [How to: Validate Using XSD](../../../../visual-basic/programming-guide/concepts/linq/how-to-validate-using-xsd-linq-to-xml.md) and . - -## See also - -- [Getting Started (LINQ to XML)](../../../../visual-basic/programming-guide/concepts/linq/getting-started-linq-to-xml.md) diff --git a/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md b/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md deleted file mode 100644 index 24fdc5772b899..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-other-xml-technologies.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -title: "LINQ to XML vs. Other XML Technologies2" -ms.date: 07/20/2015 -ms.assetid: 72ce3a82-ffc6-488c-98e7-b9b40f3591ec ---- -# LINQ to XML vs. Other XML Technologies -This topic compares [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] to the following XML technologies: , XSLT, MSXML, and XmlLite. This information can help you decide which technology to use. - - For a comparison of [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] to the Document Object Model (DOM), see [LINQ to XML vs. DOM (Visual Basic)](../../../../visual-basic/programming-guide/concepts/linq/linq-to-xml-vs-dom.md). - -## LINQ to XML vs. XmlReader - is a fast, forward-only, non-caching parser. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is implemented on top of , and they are tightly integrated. However, you can also use by itself. - - 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. In this case, you would probably want to use by itself. - - 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 [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] provides. - -## LINQ to XML vs. XSLT - Both [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] and XSLT provide extensive XML document transformation capabilities. XSLT is a rule-based, declarative approach. 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 can be a very productive system that yields high-performance applications. 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. The managed XSLT engine compiles XSLT to CLR code, and performs even better in some scenarios than the native XSLT engine. - - 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. 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. - - After you have mastered [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] query expressions, [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] transformations are a powerful technology that is easy to use. Basically, you form your XML document by using functional construction, pulling in data from various sources, constructing objects dynamically, and assembling the whole into a new XML tree. The transformation can generate a completely new document. Constructing transformations in [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is relatively easy and intuitive, and the resulting code is readable. This reduces development and maintenance costs. - - [!INCLUDE[sqltecxlinq](~/includes/sqltecxlinq-md.md)] is not intended to replace XSLT. 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 has the advantage of being a World Wide Web Consortium (W3C) standard. If you have a requirement that you use only technologies that are standards, XSLT might be more appropriate. - - XSLT is XML, and therefore can be programmatically manipulated. - -## LINQ to XML vs. MSXML - MSXML is the COM-based technology for processing XML that is included with Microsoft Windows. MSXML provides a native implementation of the DOM with support for XPath and XSLT. It also contains the SAX2 non-caching, event-based parser. - - 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. MSXML can be used from any programming language that supports COM, including C++, JavaScript, and Visual Basic 6.0. - - MSXML is not recommended for use in managed code based on the common language runtime (CLR). - -## LINQ to XML vs. XmlLite - XmlLite is a non-caching, forward only, pull parser. Developers primarily use XmlLite with C++. It is not recommended for developers to use XmlLite with managed code. - - 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. 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 is not integrated with Language-Integrated Query (LINQ). It does not yield the programmer productivity improvements that are the motivating force behind LINQ. - -## See also - -- [Getting Started (LINQ to XML)](../../../../visual-basic/programming-guide/concepts/linq/getting-started-linq-to-xml.md)