Notebook

Consume a RSS feed with ASP.NET C#

If you ever find yourself wanting to display your favourite RSS feed on your website this can be achieved relatively easily by creating a reusable User Control. The control will take two parameters – feed url, and number of items to display, so this can be reused time and time again.

First, create a new User Control, I’ve called mine RSSFeed.ascx. Go to your code-behind and add the following:

//declare the properties
private string _feedUrl;
public string FeedUrl
{
  get { return _feedUrl; }
  set { _feedUrl = value; }
}

private int _items;
public int Items
{
  get { return _items; }
  set { _items = value; }
}

//initiate the XMLNameSpaceManager
private XmlNamespaceManager _xmlnsm;

//grab the feed and bind it to a repeater control called rptRssFeed
protected void Page_Init(object sender, EventArgs e)
{
  XPathNavigator xpn = new XPathDocument(_feedUrl).CreateNavigator();
  _xmlnsm = XmlHelper.GetXmlNameSpaceManager(xpn);

  rptRssFeed.DataSource = xpn.Select("/x:rss/x:channel/x:item", _xmlnsm);
  rptRssFeed.DataBind();
}

//assign the feed values to controls within the repeater
protected void rptRssFeed_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
    XPathNavigator xpn = (XPathNavigator)e.Item.DataItem;
    Literal litTitle = (Literal)e.Item.FindControl("litTitle");
    Literal litDescription = (Literal)e.Item.FindControl("litDescription");
    HyperLink hypLink1 = (HyperLink)e.Item.FindControl("hypLink1");
    HyperLink hypLink2 = (HyperLink)e.Item.FindControl("hypLink2");

    litTitle.Text = xpn.SelectSingleNode("x:title", _xmlnsm).Value;
    litDescription.Text = xpn.SelectSingleNode("x:description", _xmlnsm).Value;
    hypLink1.NavigateUrl = xpn.SelectSingleNode("x:link", _xmlnsm).Value;
    hypLink2.NavigateUrl = xpn.SelectSingleNode("x:link", _xmlnsm).Value;
  }
}

All that is needed in the design view of the User Control is the following:

<asp:Repeater ID="rptRssFeed" runat="server" onitemdatabound="rptRssFeed_ItemDataBound">
  <ItemTemplate>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="<%# (Container.ItemIndex < Items) %>">
      <asp:HyperLink Target="_blank" ID="hypLink2" Text="View" runat="server"><img src="images/view-button.png" alt="view full story" /></asp:HyperLink>
      <p><asp:HyperLink Target="_blank" ID="hyplink1" runat="server"><asp:Literal ID="litTitle" runat="server" /></asp:HyperLink></p>
      <p><asp:Literal ID="litDescription" runat="server" /></p>
      <hr />
    </asp:PlaceHolder>
  </ItemTemplate>
</asp:Repeater>

The code above simply lists the headlines with a short description, and there is a view button for clicking through to the full story.

To use this User Control in your pages you can simply drag and drop it from the Solution Explorer in to the Design View of your aspx page. This will add the control reference to the page automatically. Supply the two properties and you are good to go:

<uc1:RSSFeed ID="RSSFeed1" FeedUrl="http://feed.url.xml" Items="5" runat="server" />

That’s it. A simple reusable RSS Feed User Control.

Like it?