RSS stands for "Really Simple Syndication". It is an XML file format which allows content creators to publish/syndicate content to their users. The users can subscribe to this content using their browsers or other desktop applications. This subscription allows the users to view and organize the updated content in their browsers or other desktop applications without actually visiting the website. They can then decide to visit the website and view the article(s) that has been updated in the feed. Users can subscribe to RSS feeds from multiple websites and see only the content that has been updated since their last visit to those websites or since the time they last viewed their contents in the feeds. After reading this article, you will be able to create an RSS feed for your website using a single ASP.NET page.
In this article, we will create an RSS XML file dynamically from an ASP.NET page displaying a list of latest 10 articles published at Stardeveloper.com. We will cache this list in Cache object to improve performance. This RSS file will be updated every 60 minutes. The users will be able to subscribe to this feed using their browsers. All modern browsers display an RSS icon when the user is visiting a website that makes available a feed. Upon clicking that RSS icon, the feed is displayed in the browser. How does a browser know that a website is making available a feed? We'll come to that later in the article.
Stardeveloper.com RSS Web Feed
Before we proceed any further, let us look at what you see in your browser when you access home . In the Safari web browser, you can clearly see a blue icon displaying RSS as shown below:

When you click on this blue RSS icon, a page opens up showing you information about latest articles at Stardeveloper.com:

Creating an RSS ASP.NET Web Feed
Create a new text file and save it as "rss.aspx" in your ASP.NET web application. For Stardeveloper.com, I placed it in "/articles" sub-folder of the main application. Copy and paste following code in it:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object source, EventArgs e)
{
if (Cache["Rss"] == null)
{
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
string channelTitle = "Stardeveloper.com Headlines";
string channelLink = "http://www.stardeveloper.com";
string channelDesc = "Get latest articles and information about ASP, " +
"ASP.NET and JSP from Stardeveloper.com.";
string language = "en-us";
int ttl = 60; // Time to live, in minutes.
string copyright = "Copyright 1999 - " + DateTime.Now.Year +
" Stardeveloper.com, All Rights Reserved.";
string connstr = ConfigurationManager.ConnectionStrings["YourConnStr"]
.ConnectionString;
string sql = "SELECT TOP 10 ArticleID, Title, Body, DatePublished, " +
"Author FROM Articles ORDER BY DatePublished DESC";
StringBuilder buffer = new StringBuilder();
buffer.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
buffer.Append("<rss version=\"2.0\">\r\n");
buffer.Append("<channel>\r\n");
buffer.Append("<title>").Append(channelTitle).Append("</title>\r\n");
buffer.Append("<link>").Append(channelLink).Append("</link>\r\n");
buffer.Append("<description>").Append(channelDesc);
buffer.Append("</description>\r\n");
buffer.Append("<language>").Append(language).Append("</language>\r\n");
buffer.Append("<ttl>").Append(ttl).Append("</ttl>\r\n");
buffer.Append("<copyright>").Append(copyright);
buffer.Append("</copyright>\r\n");
using (SqlConnection con = new SqlConnection(connstr))
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleResult);
if (dr.HasRows)
{
bool firstRow = true;
while (dr.Read())
{
int articleId = dr.GetInt32(0);
string articleTitle = dr.GetString(1);
string articleSynopsis = dr.GetString(2);
DateTime datePublished = dr.GetDateTime(3);
string author = dr.GetString(4);
if (firstRow == true)
{
buffer.Append("<lastBuildDate>");
buffer.Append(datePublished.ToString("r"));
buffer.Append("</lastBuildDate>\r\n");
}
buffer.Append("<item>\r\n");
buffer.Append("<title>").Append(articleTitle);
buffer.Append("</title>\r\n");
buffer.Append("<link><![CDATA[");
buffer.Append(String.Format("http://www.stardeveloper.com/" +
"articles/display.html?article={0}&page=1", articleId));
buffer.Append("]]></link>\r\n");
buffer.Append("<description><![CDATA[");
buffer.Append(articleSynopsis);
buffer.Append("]]></description>\r\n");
buffer.Append("<author>").Append(author).Append("</author>\r\n");
buffer.Append("<pubDate>");
buffer.Append(datePublished.ToString("r"));
buffer.Append("</pubDate>\r\n");
buffer.Append("</item>\r\n");
firstRow = false;
}
}
}
buffer.Append("</channel>\r\n");
buffer.Append("</rss>\r\n");
Cache.Insert("Rss", buffer.ToString(), null,
DateTime.Now.AddHours(1), TimeSpan.Zero);
}
Response.Write(Cache["Rss"].ToString());
}
</script>Explanation
This code first imports the necessary namespaces. Then in thePage_Load()event, we set theContentTypeof the page to "text/xml", since we will be creating an RSS XML file. We also set the encoding to UTF-8. Next, we check theCacheobject to see if we have previously saved this RSS file in memory, if not then we start the process from beginning.Structure of an RSS 2.0 XML File
The structure of RSS 2.0 XML file that we will create dynamically will be like this:<?xml version=\"1.0\" encoding=\"utf-8\"?> <rss version=\"2.0\"> <channel> <title></title> <link></link> <description></description> <language></language> <ttl></ttl> <copyright></copyright> <lastBuildDate></lastBuildDate> <item> <title></title> <link></link> <description></description> <author></author> <pubDate></pubDate> </item> ... </channel> </rss>An RSS 2.0 file consists of "xml" and "encoding" info at the first line, just like any XML file. After that we encapsulate rest of the content within a "rss" tag. The attribute 'version' is set to '2.0' identifying the version of RSS that this feed adheres to.
Next, we place everything inside a "channel" tag. A channel has title, link, description, language, ttl (time to live), copyright and lastBuildDate tags. For a complete list of tags that you can place inside "channel" tag, please visit this link.
For the list of 10 latest articles, we add an "item" tag and place the details there. "item" tag also has title, link, description, author and pubDate tags. An "item" tag should be placed for each item in your feed.
Now that we understand the structure of the RSS feed we will be creating dynamically from our ASP.NET page, we go back to the code of 'rss.aspx' page that we were creating.
To create this RSS XML file in memory, we create a
StringBuilderobject and append the strings that we create to it in the required sequence. Rest of the code is quite self-explanatory. We keep appending the tags as is required by RSS specification. In the middle when it comes to appending the information for the latest 10 articles, we connect to the database, retrieve top 10 articles, and append their info to theStringBuilderobject, one at a time.Note: The T-SQL string given in the code is not the exact T-SQL statement executed at Stardeveloper.com. For the purpose of simplification, I have changed it to a simpler statement so that the reader can understand what is going on.Lastly, when the file has been correctly created in the
StringBuilderobject in memory, we save it in theCacheobject, only to be refreshed every 60 minutes. We then write its contents to the client browser.Demonstration
To see what the code in rss.aspx page produces, visit Stardeveloper RSS Feed.Note: For Stardeveloper.com, you can also enter your email address at the top-left "Stay Informed" box and be notified via email every time this feed is updated.
Inserting the RSS Feed Meta-Tag
To make the visiting browser know that a feed exists for our website, we insert following snippet of code in the head section (between <head> and </head> tags):<link rel="alternate" title="Stardeveloper.com Headlines" href="http://www.stardeveloper.com/articles/rss.aspx" type="application/rss+xml">That is all. We created the RSS feed ASP.NET page and inserted the HTML code snippet in all the pages of our website. Now the visiting user will be able to know about the feed when he visits our website and sees the blue RSS icon. Upon clicking that icon, RSS feed page will be displayed.
Summary
We learned what an RSS feed is and how RSS can be used to ceate a feed dynamically from an ASP.NET web page. In this article, I explained how the RSS feed at Stardeveloper.com works by explaining its code in detail. We also learned how article info from database is used to populate the feed. Lastly, we learned about caching this feed in memory to improve performance.I hope after reading this article, you will be able to create a feed for your website/blog. Good luck!
discuss this topic to forum
