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

Back in Part 1, we put together the first part of our application. We defined a list of RSS feeds and pulled in the list into a drop-down list box. In this post, we’ll get our selection from the user and build a list of atricles from the RSS feed data.

Entry Data Storage

The first step is to decide what data we want to display. In our example, we’ll display the following fields:

  • Title
  • Published Date
  • Tags

Along with these fields, we’ll need to know the entry’s URL so that we can allow the user to display the blog entry. As with the feed definitions, we’ll store this information in a basic class:

public class BlogEntryDefinition
{
   public string Title { get; set; }
   public string Url { get; set; }
   public IEnumerable Tags { get; set; }
   public DateTime Published { get; set; }
}

Getting The User’s Selection

Before we can process the feed data, we need to know what feed the user wants to display. In our display, we placed an Open button next to our combo box. When the user presses this button, this will let our program know when to process the selection.

We must fist update our display code for the button, specifying an event handler method that will be called when the user clicks on the button. We do this by specifying a method name for the OnClick parameter:

<asp:Button ID="OpenButton"
            Text="Open"
            OnClick="OpenButton_OnClick"
            runat="server" />

Back in our code-behind, we need to define the method:

protected void OpenButton_OnClick(object sender, EventArgs e)
{
}

When the user clicks on the button, this method will be called. The sender parameter will be the object that initiated the OnClick event, in this case our button, and the e parameter will contain the arguments for the event. In our example, we won’t use these parameters.

Loading The Blog Entry Definitions

To load up the list of blog entries, we process the data in the same way that we did when we loaded up our feed definitions. The only difference is that rather than getting the data from a file on our web server, we are loading the data from a URL. We get the URL from our drop-down list box control through the SelectedValue property:

XDocument rssFeed = XDocument.Load(FeedList.SelectedValue);
var posts = from item in rssFeed.Descendants("item")
select new BlogEntryDefinition()
{
   Title = (string)item.Element("title"),
   Url = (string)item.Element("link"),
   Published = DateTime.Parse((string)item.Element("pubDate")),
   Tags = from category in item.Elements("category")
          orderby (string)category
          select (string)category
};

Building The Display

There are multiple ways in which you could display the data. In our example, we’ll use a grid view control. We’ll manually define the columns of the control so that we can specify custom cell values:

<asp:GridView ID="FeedDetailsGrid"
              runat="server"
              AutoGenerateColumns="false"
              Font-Name="Verdana"
              Font-Size="10pt"
              Cellpadding="4"
              HeaderStyle-BackColor="#444444"
              HeaderStyle-ForeColor="White"
              AlternatingRowStyle-BackColor="#dddddd">
              <Columns>
                <asp:TemplateField>
                  <HeaderTemplate>Published</HeaderTemplate>
                  <ItemTemplate>
                    <%#Eval("Published", "{0:G}") %>
                  </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                  <HeaderTemplate>Article</HeaderTemplate>
                  <ItemTemplate>
                    <a href='<%#Eval("Url") %>' target="_blank"><%#Eval("Title") %></a>
                  </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                  <HeaderTemplate>Tags</HeaderTemplate>
                  <ItemTemplate>
                    <asp:Repeater ID="TagRepeater"
                                  DataSource='<%#Eval("Tags") %>'
                                  runat="server">
                      <ItemTemplate>
                        <%#Container.DataItem %>
                      </ItemTemplate>
                      <SeparatorTemplate>,</SeparatorTemplate>
                    </asp:Repeater>
                  </ItemTemplate>
                </asp:TemplateField>
              </Columns>
</asp:GridView>

Putting It Together

First we handled the processing when the user clicked on the Open button. Then we built a display to contain all the data. Now, we need tell our grid view control what data it will be displaying. This process is the same as it was with the drop-down list box control:

FeedDetailsGrid.DataSource = feeds;
FeedDetailsGrid.DataBind();

Now, when we click on the Open button, we get a nicely formatted grid with the list of blog entries from the selected feed:

Conclusion

As you’ve seen through these two posts, with just a little bit of code and a few screen controls, you can create a simple RSS reader. ASP.NET gives you a simple framework to leverage the .NET Framework to create a feature-rich website.