Chapter 12
LINQ to XML
WHAT YOU WILL LEARN IN THIS CHAPTER:
- What LINQ is and how it is used
- Why you need LINQ to XML
- The basic LINQ to XML process
- More advanced features of LINQ to XML
- XML Literals in .NET
So far you’ve seen a number of ways that you can read, process, and create XML. You can use the document object model (DOM), which loads the whole document into memory, or one of the streaming methods covered in the previous chapter, such as Microsoft’s XmlReader or the SAX interface. This chapter presents yet another option, which unifies the task of interacting with XML with one of Microsoft’s core programming technologies, LINQ.
WHAT IS LINQ?
One aim of most programming languages is to be consistent. One area in which most languages fail in this respect is querying. The codes to query a database, a collection of objects, and an XML file are radically different. Microsoft has tried to abstract the querying process so that these, and other data sources, can be treated in a similar fashion. To this end, Microsoft invented Language Integrated Query, or LINQ.
LINQ is loosely based on SQL (the standard way to query a relational database), but gives you two ways to specify your query. The first, and some would say easier of the two because it tries to imitate natural language, takes the following form:
from <range variable> in <collection> where <predicate> select <something using the range variable>
Here, range variable is a standard identifier that is used to refer to the items ...
Get Beginning XML, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.