Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Consume an RSS Feed in an ASP.NET Web Page

0.00/5 (No votes)
10 Apr 2015 1  
Consuming / displaying an RSS feed on an ASP.NET web page

Introduction

I recently had a requirement to display the contents from an RSS feed on a web page. In fact, this was to display the contents of my very own CodeProject blog on my web site.

Displaying an RSS feed on a web site is relatively simple. In its simplest terms, an RSS feed is an XML structure that contains data which can be displayed on a web page. RSS feeds are used by many different kinds of web sites to push information. Users who are interested in that information can then subscribe to those RSS feeds by consuming them.

Structure of an RSS Feed

RSS feeds have the following basic structure. Other information is contained within them but for the purposes of brevity, these have been removed.

XML
<rss>
  <channel>
    <item>
      <title></title>
      <link></link>
      <description></description>
      <pubDate></pubDate>
    </item>
  </channel>
</rss>

The information that I was interested in for my web site were the Title, Link, Description and Publication Date items from the RSS feed.

Consuming an RSS Feed

There are several different ways to achieve this, so this is just one way. I have implemented this using an ASP.NET web form but the general principle can be applied to other technologies.

  1. Firstly, I added a Gridview control to my web page. A Repeater control is also perfectly acceptable.
    ASP.NET
    <asp:GridView ID="gvRss" runat="server">
      <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
              </tr>
              <tr>
                <%#Eval("PublishDate") %>
              </tr>
              <tr>
                <td colspan="2">
                  <hr />
                  <%#Eval("Description") %>
                </td>
              </tr>
              <tr>
                <td> </td>
                <td align="right">
                  <a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
                </td>
              </tr>
            </table>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  2. In the code behind, I have added a new method to populate the Gridview control with data from the RSS feed as follows:
    C#
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PopulateRssFeed();
    }
    
    private void PopulateRssFeed()
    {
        string rssFeedUrl = ConfigurationManager.AppSettings["RssFeedUrl"];
        List<feeds> feeds = new List<feeds>();
        XDocument xDoc = XDocument.Load(rssFeedUrl);
        var items = (from x in xDoc.Descendants("item")
                     select new
                         {
                             title = x.Element("title").Value,
                             link = x.Element("link").Value,
                             pubDate = x.Element("pubDate").Value,
                             description = x.Element("description").Value
                         });
        if (items != null)
        {
            feeds.AddRange(items.Select(i => new Feeds
                {
                    Title = i.title, Link = i.link, PublishDate = i.pubDate, Description = i.description
                }));
        }
        gvRss.DataSource = feeds;
        gvRss.DataBind();
    }
  3. I then created a simple POCO Feed class that mapped the RSS feed items to the Gridview control.
    C#
    public class Feeds
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string PublishDate { get; set; }
        public string Description { get; set; }
    }
  4. I store the URL to my RSS feed in the web.config. This ensures that the URL can be easily changed if necessary.
    XML
    <appSettings>
      <add key="RssFeedUrl" value="http://www.somedomain.com/rssfeed.rss"/>
    </appSettings>

Summary

And that's all there is to it. As can be seen from the link I provided, you can view the RSS feed yourself on my web site.

If you are likely to display multiple RSS feeds, then you can easily create a User Control and add the Gridview to this. To make the User Control reusable, you could add a public property to the User Control to set the RSS feed URL. You then add the User Control to your web page and set the RSS feed URL property as required.

Hopefully, this tip has given you sufficient information to start consuming RSS feeds in your own applications. Feel free to leave a comment if you would like me to further elaborate on anything within this tip.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here