This section details how to program with LINQ to XML independent of Language-Integrated Query. In DOM XML nodes, including elements and attributes, must be created in the context of an XML document.Because LINQ to XML provides a fully featured in-memory XML programming API you can do all of the things you would expect when reading and manipulating XML. Here is a fragment of the code from the previous example to create a name element: Note how the XML document is a fundamental concept in DOM.
The API choices for working with XML seem to be either aged and verbose such as DOM or XML specific such as XQuery or XSLT which require motivation, study, and time to master.
LINQ to XML, a component of the LINQ project, aims to address this issue.
For example using Xml Document (the DOM implementation from Microsoft) this would be a typical way to create an XML tree. Inner Text = "206-555-0144"; Xml Element phone2 = doc. XDocument contacts Doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("LINQ to XML Contacts XML Example"), new XProcessing Instruction("My App", "1"), new XElement("contacts", new XElement("contact", new XElement("name", "Patrick Hines"), new XElement("phone", "206-555-0144"), new XElement("address", new XElement("street1", "123 Main St"), new XElement("city", "Mercer Island"), new XElement("state", "WA"), new XElement("postal", "68042") ) ) ) ); XML names LINQ to XML goes out of its way to make XML names as straightforward as possible.
Xml Document doc = new Xml Document(); Xml Element name = doc. Inner Text = "Patrick Hines"; Xml Element phone1 = doc. Arguably, the complexity of XML names, which is often considered an advanced topic in XML literature, comes not from namespaces, which developers use regularly in programming, but from XML prefixes.
LINQ to XML is a language-agnostic component of the LINQ Project. Functional construction is described further in the section titled "Creating XML From Scratch." Document "free" When programming XML your primary focus is usually on XML elements and perhaps attributes.
The samples in most of this document are shown in C# for brevity. This makes sense because an XML tree, other than at the leaf level, is composed of XML elements and your primary goal when working with XML is traversing or manipulating the XML elements that make up the XML tree.The samples in most of this document are shown in C# for brevity.(44 printed pages) Introduction Sample XML Programming XML With LINQ to XML LINQ to XML Design Principles The LINQ to XML Class Hierarchy XML Names Loading Existing XML Creating XML From Scratch Traversing XML Manipulating XML Working With Attributes Working With Other Types of XML Nodes Annotating Nodes With User-Defined Information Outputting XML Validating XML Querying XML With LINQ to XML Querying XML Using Query Expressions with XML Using XPath and XSLT With LINQ to XML Mixing XML and Other Data Models Reading From a Database to XML Reading XML and Updating a Database Layered Technologies Over LINQ to XML LINQ to XML in Visual Basic 9.0 Schema Aware XML Programming February 2007 CTP Release Notes Changes Since the May 2006 CTP Non-Exhaustive List of Planned Features in Future Releases References XML has achieved tremendous adoption as a basis for formatting data whether in Word files, on the wire, in configuration files, or in databases; XML seems to be everywhere.LINQ to XML was designed to be a cleaner, modernized API, as well as fast and lightweight. Append Child(contacts); This style of coding provides few clues to the structure of the XML tree.LINQ to XML uses modern language features (e.g., generics and nullable types) and diverges from the DOM programming model with a variety of innovations to simplify programming against XML. LINQ to XML supports this approach to constructing an XML tree but also supports an alternative approach referred to as functional construction.This provides a consistent query experience across LINQ enabled APIs and allows you to combine XML queries and transforms with queries from other data sources.