03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

192 ❘ CHAPTER 8 LINQ<br />

For example, suppose a program has an array named employees that contains Employee objects.<br />

The following code uses LINQ and functional construction to build an XML fragment containing<br />

elements for each of the Employee objects.<br />

// Use LINQ to create a list of elements.<br />

var makeEmployees =<br />

from employee in employees<br />

select new XElement("Employee",<br />

new XAttribute("FirstName", employee.FirstName),<br />

new XAttribute("LastName", employee.LastName));<br />

// Create the XML document.<br />

XElement document = new XElement("Employees", makeEmployees);<br />

The code starts with a LINQ query that selects information from the employees array. For each<br />

Employee in the array, the query creates an XElement. It uses the Employee object’s FirstName and<br />

LastName properties to create XAttribute objects for the XElement.<br />

You can even include the LINQ query directly inside the top-level constructor, as shown in the<br />

following code.<br />

XElement document2 = new XElement("Employees",<br />

from employee in employees<br />

select new XElement("Employee",<br />

new XAttribute("FirstName", employee.FirstName),<br />

new XAttribute("LastName", employee.LastName)));<br />

LINQ out of XML<br />

The LINQ XML objects provide a standard assortment of LINQ methods that make moving data<br />

from those objects into IEnumerable objects simple. Using these functions, it’s about as easy to<br />

select data from the XML objects as it is from IEnumerable objects such as arrays and lists.<br />

XML objects represent hierarchical data. To make using that data easier, the XML classes also<br />

provide methods to help you search those data hierarchies. For example, the XElement object provides<br />

a Descendants method that searches the object’s descendants for elements of a certain type.<br />

For example, the following code searches the XElement named document for descendants named<br />

“Employee” and displays their FirstName and LastName attributes.<br />

var selectEmployee =<br />

from employee in document.Descendants("Employee")<br />

select new<br />

{<br />

FirstName = employee.Attribute("FirstName").Value,<br />

LastName = employee.Attribute("LastName").Value<br />

};<br />

foreach (var obj in selectEmployee)<br />

Console.WriteLine(obj.FirstName + " " + obj.LastName);<br />

The LINQ query selects objects from document.Descendants("Employee"). Each of the objects<br />

returned by the Descendants method is an XElement. The query uses that object’s Attribute method<br />

to get the object’s FirstName and LastName attributes. Those attributes are XAttribute objects, so<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!