Figure 14-14

Query Elements with a Namespace

If you observe the RSS document structure carefully, you notice that the <creator> element has the dc namespace defined (see Figure 14-15).

Figure 14-15

The dc namespace is defined at the top of the document, within the <rss> element (see Figure 14-16).

Figure 14-16

When using LINQ to XML to query elements defined with a namespace, you need to specify the namespace explicitly. The following example shows how you can do so using the XNamespace element and then using it in your code:

XDocument rss =

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

XNamespace dcNamespace = 'http://purl.org/dc/elements/1.1/';

var posts =

 from item in rss.Descendants('item')

 select new {

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

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

  Creator = item.Element(dcNamespace + 'creator').Value

 };

foreach (var post in posts) {

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

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

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

 Console.WriteLine();

}

Figure 14-17 shows the query result.

Figure 14-17

Retrieving Postings in the Last 10 Days

The <pubDate> element in the RSS document contains the date the posting was created. To retrieve all postings published in the last 10 days, you would need to use the Parse() method (from the DateTime class) to convert the string into a DateTime type and then deduct it from the current time. Here's how that can be done:

XDocument rss =

 XDocument.Load(

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

XNamespace dcNamespace = 'http://purl.org/dc/elements/1.1/';

var posts =

 from item in rss.Descendants('item')

 where (DateTime.Now -

  DateTime.Parse(item.Element('pubDate').Value)).Days < 10

 select new {

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

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

  Creator = item.Element(dcNamespace + 'creator').Value,

  PubDate = DateTime.Parse(item.Element('pubDate').Value)

 };

Console.WriteLine('Today's date: {0}',

 DateTime.Now.ToShortDateString());

foreach (var post in posts) {

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

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

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

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

 Console.WriteLine();

}

LINQ to SQL

LINQ to SQL is a component of the .NET Framework (v3.5) that provides a runtime infrastructure for managing relational data as objects.

With LINQ to SQL, a relational database is mapped to an object model. Instead of manipulating the database directly, developers manipulate the object model, which represents the database. After changes are made to it, the object model is submitted to the database for execution.

Visual Studio 2008 includes the new Object Relational Designer (O/R Designer), which provides a user interface for creating LINQ to SQL entity classes and relationships. It enables you to easily model and visualize a database as a LINQ to SQL object model.

Using the Object Relational Designer

To see how LINQ to SQL works, create a new Windows application using Visual Studio 2008.

First, add a new LINQ to SQL Classes item to the project. Use the default name of DataClasses1.dbml (see Figure 14-18).

Figure 14-18

In Server Explorer, open a connection to the database you want to use. For this example, use the pubs sample database. Drag and drop the following tables onto the design surface of DataClasses1.dbml:

authors

publishers

titleauthor

titles

Figure 14-19 shows the relationships among these four tables.

Figure 14-19

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

0

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

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