Pages

Tuesday, October 19, 2010

How to consume RSS in ASP.NET 3.5

After a very long gap of routine development and coding of web applications, I found this to be more interesting, easy and fascinating for me(At the least to learn something new!). Straight away we can jump into the coding.

The requirement was to consume a RSS feed and show the top 5 feeds in the page(in a sub section).

Create a Web User Control:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RssFeed.ascx.cs" Inherits="_controls_rss_feed" %>
<asp:Repeater ID="rptRssFeed" runat="server" >

    <ItemTemplate>
        <li>
            <h4><asp:Literal ID="litTitle" runat="server" Text='<%# Eval("Title") %>' /></h4> 
            <p><asp:Literal ID="litDescription" runat="server" Text='<%# Eval("Description") %>' /></p>
            <asp:HyperLink ID="hypLink" Text="Read More" runat="server" NavigateUrl='<%# Eval("Link") %>'                                  target="_blank" />
        </li>
    </ItemTemplate>    
</asp:Repeater>


Code Behind:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class _controls_RssFeed : System.Web.UI.UserControl
{
    # region Properties
    public string FeedUrl { get; set; }
    #endregion Properties
    #region Page Event
    protected void Page_Load(object sender, EventArgs e)
    {
        //Loads the RSS feed
        XElement rootElement = XElement.Load(FeedUrl);
        //Queries to pick the top 5 feeds/items order by date DESC - LINQ
        var feedItems = (from item in rootElement.Descendants("item")
                         orderby DateTime.Parse(item.Element("pubDate").Value) descending
                         select new
                         {
                             Title = item.Element("title").Value,
                             Description = item.Element("description").Value,
                             Published = DateTime.Parse(item.Element("pubDate").Value),
                             Link = item.Element("link").Value
                         }).Take(5);              
        //DataSource for the Repeater Control.
        this.rptRssFeed.DataSource = feedItems;
        this.rptRssFeed.DataBind();
    }
    #endregion Page Event
} 


So now the Re-Usable Web User Control is developed and ready to use across the application. 


Create a Page and Inherit the Web User Control: 
Just register the control and use it as as when needed. Here is the page code. 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="_controls/RssFeed.ascx" tagname="RssFeed" tagprefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server"> 
      <title></title> 
   </head>
   <body>
      <form id="form1" runat="server">
        <div>  
           <uc:RssFeed ID="ctrRssFeed" runat="server" FeedUrl="http://rss.news.yahoo.com/rss/sports" /> 
        </div>
      </form> 
    </body> 
</html>


We have successfully created a control consuming a Rss Feed and inherited the control to the page. I hope 
this would have helped you or served your purpose. 

Have fun and happy coding.