This tutorial will show you how to create a dynamic XML RSS 2.0 feed using ASP.NET 2.0 (C#). The headlines will be retrieved from a SQL database.
I don’t think there’s a need for explaining what RSS is, or what RSS does. Most of the time, the headlines of an RSS feed are retrieved from a database, such as SQL server. An example of this is Feedpedia.com which creates feeds containing the latest headlines retrieved from the website’s database. One of these feeds is located at http://www.feedpedia.com/RSS/TodaysHeadlines.aspx?Group=13.
In this tutorial we’re going to create a similar RSS feed, using ASP.NET 2.0.
In the ASP.NET 2.0 project, create a new file which will be the RSS feed. We used “TodaysHeadlines.aspx”.
Now open the markup of the file and replace everything inside it with the following two lines:
Don’t forget to change the CodeFile and Inherits attributes of the Page tag in case you gave your RSS file a different name than TodaysHeadlines.aspx.
If you’re wondering what the second page does: it tells the ASP.NET server to keep the page in cache for 120 seconds (2 minutes) so that the SQL server doesn’t have to be accessed every time someone opens your RSS feed. You can change the duration depending on how often your content updates.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TodaysHeadlines.aspx.cs" Inherits="TodaysHeadlines" %>
<%@ OutputCache Duration="120" %>
Now since we are already discussing the OutputCache, I want to bring your attention towards something. If you’re going to create a dynamic RSS feed file to which you’re going to pass a parameter which changes the content of the feed (such as the one on Feedpedia: http://www.feedpedia.com/RSS/TodaysHeadlines.aspx?Group=13), the cache you will create you some problems: when you open the RSS feed with a different attribute (in Feedpedia’s case, a different Group ID), the content will not change, but the cached one will be shown. In that case you will need to tell the cache that the content varies by that parameter, and it should build a different cache based on the parameter passed. Here’s how you can do that:
<%@ OutputCache Duration="120" VaryByParam="Group" %>
Now if you didn’t understand any of my mumbling above, don’t worry, as long as you don’t plan to pass parameters to the RSS feed, everything will work fine for you without the VaryByParam attribute.
Believe it or not, we’re done with the
markup, so switch to code view. Add the following using statements at the top:
using System.Data.SqlClient;
using System.Text;
using System.Xml;
Now what’s left to do for your RSS feed to work, is add this code in the Page_Load event:
// Clear any previous output from the buffer
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xtwFeed.WriteStartDocument();
// The mandatory rss tag
xtwFeed.WriteStartElement("rss");
xtwFeed.WriteAttributeString("version", "2.0");
// The channel tag contains RSS feed details
xtwFeed.WriteStartElement("channel");
xtwFeed.WriteElementString("title", "Feedpedia Today's World News");
xtwFeed.WriteElementString("link", "http://www.feedpedia.com");
xtwFeed.WriteElementString("description", "The latest news and journals from all over the world.");
xtwFeed.WriteElementString("copyright", "Copyright 2005 - 2006 Feedpedia.com. All rights reserved.");
// Objects needed for connecting to the SQL database
SqlConnection SqlCon;
SqlCommand SqlCom;
SqlDataReader SqlDR;
// Edit to match your connection string
SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
// Edit to match your stored procedure or SQL command
SqlCom = new SqlCommand("GetTodaysHeadlines", SqlCon);
SqlCom.CommandType = CommandType.StoredProcedure;
if (SqlCon.State == ConnectionState.Closed)
{
SqlCon.Open();
}
SqlDR = SqlCom.ExecuteReader();
// Loop through the content of the database and add them to the RSS feed
while (SqlDR.Read())
{
xtwFeed.WriteStartElement("item");
xtwFeed.WriteElementString("title", SqlDR["Title"].ToString());
xtwFeed.WriteElementString("description", SqlDR["Description"].ToString());
xtwFeed.WriteElementString("link", "http://www.feedpedia.com/View.aspx?View=" + SqlDR["ID"]);
xtwFeed.WriteElementString("pubDate", SqlDR["Date"].ToString());
xtwFeed.WriteEndElement();
}
SqlDR.Close();
SqlCon.Close();
// Close all tags
xtwFeed.WriteEndElement();
xtwFeed.WriteEndElement();
xtwFeed.WriteEndDocument();
xtwFeed.Flush();
xtwFeed.Close();
Response.End();
Of course, you’ll need to do the necessary changes so that the code matches your SQL database table. If you have any questions or doubts about the code, don’t hesitate to place a comment for this tutorial. I will answer ASAP.
If you did everything right, your RSS feed will be valid and should have a structure similar to the one below:
There are several tags which you can add to the RSS feed, such as the author of the headline, the category or an unique ID. You can find more information about this at the RSS 2.0 specification page.