Console.WriteLine(book);

}

This query generates the following output:

------

Result

------

C# 2008 Programmers' Reference

Professional Windows Vista Gadgets Programming

To retrieve all not-yet-published (NYP) books from Wrox, you can use the following query:

var query2 =

 from book in library.Descendants('Book')

 where book.Attribute('published').Value == 'NYP' &&

 book.Element('Publisher').Value=='Wrox'

 select book.Element('Title').Value;

You can shape the result of a query as you've seen in earlier sections:

var query3 =

 from book in library.Descendants('Book')

 where book.Element('Publisher').Value == 'Wrox'

 select new {

  Name = book.Element('Title').Value,

  Pub = book.Element('Publisher').Value

 };

Console.WriteLine('------');

Console.WriteLine('Result');

Console.WriteLine('------');

foreach (var book in query3) {

 Console.WriteLine('{0} ({1})', book.Name, book.Pub);

}

This code generates the following output:

------

Result

------

C# 2008 Programmers' Reference (Wrox)

Professional Windows Vista Gadgets Programming (Wrox)

Besides using an anonymous type to reshape the result, you can also pass the result to a non-anonymous type. For example, suppose that you have the following class definition:

public class Book {

 public string Name { get; set; }

 public string Pub { get; set; }

}

You can shape the result of a query to the Book class, as the following example shows:

var query4 =

 from book in library.Descendants('Book')

 where book.Element('Publisher').Value == 'Wrox'

 select new Book {

  Name = book.Element('Title').Value,

  Pub = book.Element('Publisher').Value

 };

List<Book> books = query4.ToList();

An Example Using RSS

Let's now take a look at the usefulness of LINQ to XML. Suppose that you want to build an application that downloads an RSS document, extracts the title of each posting, and displays the link to each post.

Figure 14-13 shows an example of an RSS document.

Figure 14-13 

To load an XML document directly from the Internet, you can use the Load() method from the XDocument class:

XDocument rss =

 XDocument.Load (@'http://www.wrox.com/WileyCDA/feed/RSS_WROX_ALLNEW.xml');

To retrieve the title of each posting and then reshape the result, use the following query:

var posts =

 from item in rss.Descendants('item')

 select new {

  Title = item.Element('title').Value,

  URL = item.Element('link').Value

 };

In particular, you are looking for all the <item> elements and then for each <item> element found you would extract the values of the <title> and <link> elements.

<rss>

 <channel>

  ...

  <item>

   <title>...</title>

   <link>...</link>

  </item>

  <item>

   <title>...</title>

   <link>...</link>

  </item>

  <item>

   <title>...</title>

   <link>...</link>

  </item>

  ...

Finally, print out the title and URL for each post:

foreach (var post in posts) {

 Console.WriteLine('{0}', post.Title);

 Console.WriteLine('{0}', post.URL);

 Console.WriteLine();

}

Figure 14-14 shows the output.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату