Consume an RSS Feed in an ASP.NET Web Page - CodeProject

:

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.

<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: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:
    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.
    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.
    <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.

I am a professional software engineer and architect with over eighteen years commercial development experience with a strong focus on the design and development of web and mobile applications.

I have experience of architecting scalable, distributed, high volume web applications that are accessible from multiple devices due to their responsive web design, including architecting enterprise service-oriented solutions. I have also developed enterprise mobile applications using Xamarin and Telerik Platform.

I have extensive experience using .NET, ASP.NET, Windows and Web Services, WCF, SQL Server, LINQ and other Microsoft technologies. I am also familiar with HTML, Bootstrap, Javascript (inc. JQuery and Node.js), CSS, XML, JSON, Apache Cordova, KendoUI and many other web and mobile related technologies.

I am enthusiastic about Continuous Integration, Continuous Delivery and Application Life-cycle Management having configured such environments using CruiseControl.NET, TeamCity and Team Foundation Services. I enjoy working in Agile and Test Driven Development (TDD) environments.

Outside of work I have two beautiful daughters. I enjoy cycling, running and taking the dog for long walks. I love listening to music and am a fan of Rush and the Red Hot Chilli Peppers to name a few.