Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 21 INTRODUCTION TO LINQ

LINQ to XML

Extensible Markup Language (XML) is an important method of storing and exchanging data. LINQ adds features to the language that make working with XML much easier than previous methods such as XPath and XSLT. If you’re familiar with these methods, you might be pleased to hear that LINQ to XML simplifies the creation, traversal, and manipulation of XML in a number of ways, including the following:

You can create an XML tree in a top-down fashion, with a single statement.

You can create and manipulate XML in-memory without having an XML document to contain the tree.

You can create and manipulate string nodes without having a Text subnode.

Although I won’t give a complete treatment of XML, I will start by giving a very brief introduction to it before describing some of the XML manipulation features supplied by LINQ.

Markup Languages

A markup language is a set of tags placed in a document to give information about the information in the document. That is, the markup tags are not the data of the document—they contain data about the data. Data about data is called metadata.

A markup language is a defined set of tags designed to convey particular types of metadata about the contents of a document. HTML, for example, is the most widely known markup language. The metadata in its tags contains information about how a web page should be rendered in a browser and how to navigate among the pages using the hypertext links.

While most markup languages contain a predefined set of tags, XML contains only a few defined tags, and the rest are defined by the programmer to represent whatever kinds of metadata are required by a particular document type. As long as the writer and reader of the data agree on what the tags mean, the tags can contain whatever useful information the designers want.

575

CHAPTER 21 INTRODUCTION TO LINQ

XML Basics

Data in an XML document is contained in an XML tree, which consists mainly of a set of nested elements. The element is the fundamental constituent of an XML tree. Every element has a name and can

contain data. Some can also contain other, nested elements. Elements are demarcated by opening and closing tags. Any data contained by an element must be between its opening and closing tags.

An opening tag starts with an open angle bracket, followed by the element name, followed optionally by any attributes, followed by a closing angle bracket.

<PhoneNumber>

A closing tag starts with an open angle bracket, followed by a slash character, followed by the element name, followed by a closing angle bracket.

</PhoneNumber>

An element with no content can be represented by a single tag that starts with an open angle bracket, followed by the name of the element, followed by a slash, and is terminated with a closing angle bracket.

<PhoneNumber />

The following XML fragment shows an element named EmployeeName followed by an empty element named PhoneNumber.

<EmployeeName>Sally Jones</EmployeeName>

Opening tag

Content

Closing tag

<PhoneNumber />

← Element with no content

Other important things to know about XML are the following:

XML documents must have a single root element that contains all the other elements.

XML tags must be properly nested.

Unlike HTML tags, XML tags are case sensitive.

XML attributes are name/value pairs that contain additional metadata about an element. The value part of an attribute must always be enclosed in quotation marks, which can be either double quotation marks or single quotation marks.

Whitespace within an XML document is maintained. This is unlike HTML, where whitespace is consolidated to a single space in the output.

576

CHAPTER 21 INTRODUCTION TO LINQ

The following XML document is an example of XML that contains information about two employees. This XML tree is extremely simple in order to show the elements clearly. The important things to notice about the XML tree are the following:

The tree contains a root node of type Employees that contains two child nodes of type Employee.

Each Employee node contains nodes containing the name and phone numbers of an employee.

<Employees>

<Employee>

<Name>Bob Smith</Name> <PhoneNumber>408-555-1000</PhoneNumber> <CellPhone />

</Employee>

<Employee>

<Name>Sally Jones</Name> <PhoneNumber>415-555-2000</PhoneNumber> <PhoneNumber>415-555-2001</PhoneNumber>

</Employee>

</Employees>

Figure 21-13 illustrates the hierarchical structure of the sample XML tree.

Figure 21-13. Hierarchical structure of the sample XML tree

577

CHAPTER 21 INTRODUCTION TO LINQ

The XML Classes

LINQ to XML can be used to work with XML in two ways. The first way is as a simplified XML manipulation API. The second way is to use the LINQ query facilities you’ve seen throughout the earlier part of this chapter. I’ll start by introducing the LINQ to XML API.

The LINQ to XML API consists of a number of classes that represent the components of an XML tree. The three most important classes you’ll use are XElement, XAttribute, and XDocument. There are other classes as well, but these are the main ones.

In Figure 21-13, you saw that an XML tree is a set of nested elements. Figure 21-14 shows the classes used to build an XML tree and how they can be nested.

For example, the figure shows the following:

An XDocument node can have the following as its direct child nodes:

At most, one of each of the following node types: an XDeclaration node, an XDocumentType node, and an XElement node

Any number of XProcessingInstruction nodes

If there is a top-level XElement node under the XDocument, it is the root of the rest of the elements in the XML tree.

The root element can in turn contain any number of nested XElement, XComment, or XProcessingInstruction nodes, nested to any level.

Figure 21-14. The containment structure of XML nodes

Except for the XAttribute class, most of the classes used to create an XML tree are derived from a class called XNode and are referred to generically in the literature as “XNodes.” Figure 21-14 shows the XNode classes in white clouds, while the XAttribute class is shown in a gray cloud.

578

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]