Creating an RSS Reader Using ASP.NET and LINQ – Part 1

With every release of the .NET Framework, more power has been placed in the developer’s hands.  We can now concentrate on the overall process of our applications rather than developing the low-level functionality.  In this post, I’ll start creating a simple RSS reader using ASP.NET and LINQ.

Feed Definitions

The first step is to decide how to define the RSS feed information and how our program will use it.  The basic information we’re going to need will be:

  • The name of the feed.
  • The URL of the feed.

For ease of maintenance, and to allow updating the list without having to edit our source code, we’ll store the feed definitions in an external file.  There are many options for this and we’ll use the easiest option: XML.  Our file will have a basic format, only storing the basic information that we need:

<!--?xml version="1.0" encoding="utf-8" ?-->
<Feeds>
  <Feed>
    <Name>Stuck In Customs</Name>
    <Url>http://www.stuckincustoms.com/feed/</Url>
  </Feed>
  <Feed>
    <Name>2000 Things You Should Know About C#</Name>
    <Url>http://csharp.2000things.com/feed/</Url>
  </Feed>
  <Feed>
    <Name>There, I Fixed It!</Name>
    <Url>http://feeds.feedburner.com/ThereIFixedIt</Url>
  </Feed>
</Feeds>

We’ll store this information in a basic class:

public class FeedDefinition
{
   public string Name { get; set; }
   public string Url { get; set; }
}

 

Loading the Feed Definitions

Now that we have our feed definitions stored in an external file and a class defined to store the information, we need to load that data into our application. Before we had the .NET Framework, we would have to read in the file and create our own XML parser in order to load the data. This process has become easier with each version of the .NET Framework.

By utilizing LINQ, the process of obtaining the data from an XML file has become quite easy:

XDocument feedDefinitions = XDocument.Load(Server.MapPath("Feeds.xml"));
var feeds = from feed in feedDefinitions.Descendants("Feed")
select new FeedDefinition()
{
  Name = (string)feed.Element("Name"),
  Url = (string)feed.Element("Url"),
};

Using just one line of code (line 1), the entire feed definition file is loaded and parsed for you. The contents are stored in an XDocument object.  Processing this object using LINQ (lines 3-8), we obtain a collection of FeedDefinition objects (IEnumerable<FeedDefinition>) that we will use in our web page.

Building The Display

Now that we have our data, we need to be able to display it to the user. We will use a drop-down list control that will allow the user to select a feed for reading. In our ASP.NET content page, we’ll define the our display as this:

<div class="feedList">
Feed:
<asp:DropDownList ID="FeedList"
                  DataValueField="Url"
                  DataTextField="Name"
                  runat="server" />
<asp:Button ID="OpenButton"
            Text="Open"
            runat="server" />
</div>

The drop-down list box will be bound to our feed data that we read in earlier. However, we have two fields in our data. Using the DropDownList control, we can display the Name property to the user and use the Url property for processing later on.

Putting It Together

Now that we have our feed definitions and our display, we just need to put them both together:

FeedList.DataSource = feeds;

FeedList.DataBind();

This code binds our feed definition collection to the drop-down list control. All we need to do now is put our code into our content page’s Page_Init method so that it’ll populate our control the first time our page is loaded.

protected void Page_Init()
{
XDocument feedDefinitions = XDocument.Load(Server.MapPath("Feeds.xml"));
var feeds = from feed in feedDefinitions.Descendants("Feed")
select new FeedDefinition()
{
   Name = (string)feed.Element("Name"),
   Url = (string)feed.Element("Url"),
};
FeedList.DataSource = feeds;
FeedList.DataBind();
}

It’s important that you load the feed definitions in Page_Init() rather than in Page_Load().  Putting them in Page_Init() will prevent the drop-down list control from getting reinitialized during postback when we implement our Open button in Part 2.

When we load our website, we get our list of feeds neatly displayed in our control:

Conclusion

In this post, we wrote the first half of our simple RSS reader web application. By using the .NET Framework and the LINQ feature, we were able to load a list of RSS feeds into our web page without writing a lot of code.

In part 2, we’ll show how to get a list of posts from the feed and display them along with other data related to the posts.