Eric Bergman-Terrell's Blog

ASP.NET Tip: How to add an RSS feed to your website
October 4, 2010

Many websites have RSS feeds, especially websites with blogs. An RSS feed identifies content on your website, including the publication date. Web browsers and blog readers use the RSS feed to notify users when content is updated.

The acronym RSS stands for "really simple syndication", and the "really simple" part is no exaggeration. Thanks to the simplicity of RSS it's very easy to add an RSS feed to your website. You can read about the RSS specification here.

Here's what my RSS feed looks like. Pretty simple, eh?

<rss version="2.0">
  <!-- generated 7/18/2006 9:38:31 AM -->
  <channel>
    <title>Eric Bergman-Terrell's Blog</title>
    <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx</link>
    <description>by Eric Bergman-Terrell</description>
    <item>
      <title>Added an RSS feed</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=7</link>
      <description>Added an RSS feed</description>
      <pubDate>2006-07-16T00:00:00</pubDate>
    </item>
    <item>
      <title>Lady and the Stamp</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=5</link>
      <description>Lady and the Stamp</description>
      <pubDate>2006-07-13T00:00:00</pubDate>
    </item>
    <item>
      <title>Lutz Roeder's Reflector is an essential .NET develoment tool</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=4</link>
      <description>Lutz Roeder's Reflector is an essential .NET develoment tool</description>
      <pubDate>2006-07-09T00:00:00</pubDate>
    </item>
    <item>
      <title>Why I Love Firefox</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=3</link>
      <description>Why I Love Firefox</description>
      <pubDate>2006-06-15T00:00:00</pubDate>
    </item>
    <item>
      <title>Articles are now on-line</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=2</link>
      <description>Articles are now on-line</description>
      <pubDate>2006-06-02T07:58:00</pubDate>
    </item>
    <item>
      <title>New Web Host</title>
      <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx?b=1</link>
      <description>New Web Host</description>
      <pubDate>2006-06-01T21:03:00</pubDate>
    </item>
  </channel>
</rss>

Here's how to add an RSS feed to your website:

Step 1: Create an .aspx page (e.g. rss.aspx) that returns information about your content in RSS format. This .aspx page will return data in XML format, not HTML. I recommend using output caching on this page since it's likely to be retrieved frequently. I cache my rss feed for one minute:

 <%@ OutputCache Duration="60" VaryByParam="none" %> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="rss.aspx.cs" Inherits="rss" %> 

In the code-behind for the .aspx page, just build your RSS data and output it with Response.Write.

For example, here's my code-behind. The code first calls a stored procedure named pm_GET_BLOG_POSTS to get all the data about my blog. The data is stored in a dataset. Then the code iterates through each row in the dataset and builds the section of the RSS XML. Finally, the items are inserted into the XML and the XML is output using Response.Write():

...

public partial class rss : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet entriesDataSet = new DataSet();

        using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        using (SqlCommand sqlCommand = new SqlCommand())
        using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
        {
            sqlCommand.Connection = sqlConnection;

            sqlCommand.CommandText = "pm_GET_BLOG_POSTS";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlDataAdapter.Fill(entriesDataSet);
        }

        const string itemFormat = 
@"
    <item>
      <title>{0}</title>
      <link>{1}</link>
      <description>{2}</description>
      <pubDate>{3}</pubDate>
    </item>";

        string items = string.Empty;

        foreach (DataRow row in entriesDataSet.Tables[0].Rows)
        {
            string url = string.Format("http://www.PersonalMicroCosms.com/Pages/blog.aspx?b={0}", row["ID"]);
            DateTime pubDate = (DateTime) row["TimeStamp"];

            string item = string.Format(itemFormat, row["Title"], url, row["Title"], pubDate.ToString("s"));

            items = item + items;
        }

        const string rssFormatString = 
@"<rss version=""2.0"">
  <!-- generated {0} -->
  <channel>
    <title>Eric Bergman-Terrell's Blog</title>
    <link>http://www.PersonalMicroCosms.com/Pages/blog.aspx</link>
    <description>by Eric Bergman-Terrell</description>
    {1}
  </channel>
</rss>";

        string result = string.Format(rssFormatString, DateTime.Now, items);

        Response.Clear();
        Response.BufferOutput = true;
        Response.ContentType  = "text/xml";
        Response.StatusCode   = 200;

        Response.Write(result);
    }
}

Step 2: Now that you have a .aspx page that returns your RSS data, you need to notify web browsers and blog readers that the feed exists. Just add atag to the

section of your .aspx pages. For example, here's mytag:

<head runat="server">
    ...
    <link rel="alternate" type="application/rss+xml" title="RSS" href="../rss.aspx" runat="server" id="rss_link" visible="false" />
    ....
</head>

The above <link> tag lives in a master page. By default, the <link> tag is not displayed (because of the visible-"false" attribute). On my blog.aspx and blog_archive.aspx pages I enable the <link> tag as follows:

public partial class Pages_blog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlLink rssLink = Master.FindControl("rss_link") as HtmlLink;

        if (rssLink != null)
        {
            rssLink.Visible = true;
        }
    }
}
Keywords: RSS, Really Simple Syndication, ASP.NET

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015