Using LINQ to XML (and how to build a custom RSS Feed Reader with it)
page 5 of 9
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36082/ 128

Using LINQ to XML to Retrieve a Remote RSS XML Feed

The XDocument.Load(path) static method supports the ability open both XML files from the file-system, as well as remote XML feeds returned from an HTTP URL.  This enables you to use it to access remote RSS feeds, REST APIs, as well as any other XML feed published on the web.

For an example of this in action, let's take a look at the XML of my blog's RSS feed (http://weblogs.asp.net/scottgu/rss.aspx):

Figure 10

I could write the LINQ to XML code below to retrieve the above blog post data from my RSS feed, and work with the individual feed items as .NET objects:

Figure 11

Note above how I am converting the "Published" field in the RSS field - which is a string in the XML - to a .NET DateTime object.  Notice also how LINQ to XML includes a built-in XNamespace type that provides a type-safe way to declare and work with XML Namespaces (which I need to-do to retrieve the <slash:comments> element).

I could then take advantage of the composition features of LINQ to perform a further sub-query on the result, so that I filter over only those RSS posts that were published within the last 7 days using the code below:

Figure 12

As you can see above, you can feed the results of one LINQ query expression to be the input of another LINQ expression.   This enables you to write very clean, highly composable, code.


View Entire Article

User Comments

Title: Using LINQ Sub-Queries within a LINQ to XML Query Expression   
Name: Ken Jinks
Date: 2009-07-10 1:27:32 PM
Comment:
How would you incorporate a where clause into the subquery such that you only retrieve those items with a particular TAG value?

thanks
kenneth.jinks@qinetiq-na.com
Title: How to integrate with XLINQ in eixsting applications   
Name: Senthil
Date: 2008-12-17 12:21:13 AM
Comment:
How to integrate with XLINQ in eixsting applications






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 12:25:44 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search