From the course: .NET Essentials: LINQ for XML

Understand LINQ for XML - .NET Tutorial

From the course: .NET Essentials: LINQ for XML

Start my 1-month free trial

Understand LINQ for XML

- [Instructor] People like to store information in text files. Because our industry likes standards, we also create specifications on how to mark up the data within the text file. There are many standards to choose from, but XML, JSON, CSV and YAML are historically important and are used in applications every day. Here's a list of what those acronyms mean if you're not familiar with the terms. XML and JSON have fundamental features that reflect how computer applications organize data. For example, they support hierarchical data structures where information is nested in a tree-like manner. We can express subordinate relationships between data objects. XML gained prominence in the 1990s and you still see it widely used in enterprises and organizations. JSON is now a widely adopted standard. It has some noteworthy advantages over other markup standards like XML. It is very popular in web applications and web APIs. This course focuses on LINQ to XML. It is part of Microsoft .NET, so you have it freely available in any .NET project. LINQ to JSON is available from Newtonsoft. It is a popular, free, high-performance JSON framework for .NET. Use the nuget package manager to add it to your .NET project. .NET provides several APIs for working with XML data. Let's look at a few examples that pre-date LINQ to XML. For example, there is the XmlReader. It is a fast, forward-only, non-caching XML parser. Traditionally, it is used to move through XML data and read the contents of an XML node. It is still useful, though you don't need to use it directly. It is the driving engine behind LINQ to XML. Before LINQ to XML, when you wanted to manipulate the structure of an XML file, you'd reach for the XmlDocument class. It was the Microsoft API for working with the W3C Document Object Model, or DOM. That's the DOM for XML. It was full featured. You could add XML nodes, like elements and attributes and comments; you could remove or reposition nodes. And of course, you could read and write values. Prior to LINQ, XPath was the prescribed way to query an XML tree. It could return a collection of elements, a collection of attributes, and find an element or attribute value. These classes were functional, but needed an overhaul. Developers complained that XmlDocument had a non-intuitive, awkward syntax and was too verbose. XPath was cumbersome and lacking in areas. For example, it was difficult to project XPath results into types. The expressions were string-based, with all the associated trouble that exists with that model. A simple typo could ruin a query and the .NET compiler couldn't help determine what was wrong. Let's not dwell on those old APIs any longer. Microsoft found better solutions and put them into LINQ to XML. LINQ to XML provides an in-memory, XML-programming interface that leverages the .NET LINQ framework. The most important advantage to LINQ to XML is its integration with LINQ. This integration enables you to write queries in the in-memory XML document, and you can retrieve collections of elements and attributes. The query capability of LINQ to XML is comparable in functionality, though not in syntax, to XPath. The integration of LINQ to C# and Visual Basic provides stronger typing, compiled-time checking and improved debugger support. LINQ to XML uses .NET capabilities and is comparable to an updated, redesigned Dom XML programming interface. So you get two parts: You get one part for querying the data and you get one part for working with the XML Dom. Other benefits: You can load or serialize XML into files or streams. You can also validate the XML trees using XSD and you can create XML from scratch by using functional construction. It's time to get acquainted with the LINQ to XML classes. There are about 20 classes in the library. We'll look at the most common ones. We'll start by looking at the abstract class at the root. It is called XObject. It represents a node or an attribute in an XML tree. Below that we have another abstract class called XNode. This represents an element, a comment, a document, a processing instruction, or text node in the XML tree. The first concrete class we see in this tree is XAttribute. It represents an XML attribute, which, as you know, is a name value pair associated with an XML element. Next, we have XComment, which represents a comment in the XML. Now we get to the base class that enables the hierarchical nature of XML. It's called the XContainer class. It represents a node that can contain other nodes. You can see there's two derive types: XDocument and XElement. XDocument represents the entire document, where XElement represents one of the elements in the XML file. This is the main entry point into working with XML elements. So these are the three most common XML classes. The ones I'll use in this course, Xelement, XAttribute and XComment. Take this example XML file. Load the entire file into an XDocument, and that would include the XML declaration on line one, the comments on line two, the root know which is on line three, and all of the child (indistinct). We could represent the element on line three, WebColors, with an XElement class. And you can see that it contains two child elements. There's one on line five and there's one on line 16. So if you have a reference to an XElement for WebColors, it would have a collection of color elements. So now we can start querying the data that we find in those child elements. Take a look at line five. There's the color element. It has two attributes: color-name and color-family. Those are represented with XAttribute. And then of course, any of these other elements, like line six, like line nine, or line 10. Those can all be stored in an XElement. If you look at the one on line 10, that has three children elements, so you should be able to query or manipulate the XML that's within that HSL element. Now that we understand some of the basic concepts, let's get started. We'll look at how to load XML content into the XDocument and the XElements classes. We'll see how to walk up and down the XML tree and get the information we need. Then we'll explore the LINQ query operators and see how to query the data in the tree. Finally, we'll look at how to create and edit the XML structure so we can build an XML file and code or rearrange the XML tree elements.

Contents