Introduction
This article explains the concept of creating and publishing RSS feeds on your websites by dynamically and auto updating RSS contents driven from your database.
Many of the web application this days update their website contents frequently to attract the users at the maximum extent. Even web publishers needs to share their content without re-creating it on any form. But there is no way to inform the users about the new contents in their website. Either they have send newsletters to the users or the users have to bookmark the website URL in their browser’s favorites, which had its own drawbacks. To overcome this difficulty, Netscape introduced the concept of RSS in late 90s. It serves the information consumers and publishers to stay up to date with their desired website's content.
What is RSS?
RSS, can be expanded as Really Simple Syndication. RSS is a lightweight XML format for distributing regularly changed web contents among different web sites and users. A Web site can allow the users or other sites to publish some of its content by creating an RSS feeds.
Once you create the RSS Feeds, all your website users can subscribe the feeds and publish the feeds into their news reader or into their website. So instead of having to browse each individual site for the news, they can just import into their RSS reader all the RSS feeds. A good example of site that using RSS heavily is Google News. With google news, you can see news from around the world in one simple page. The main concept behind the Google News is subscribe RSS feeds from all major news provider around the world. Lots of article driven websites these days will provide RSS feeds on their website.
RSS Feeds Structure
If you check most of the website RSS Feeds, you can see that the output of the Feeds is just an XML File with certain tags. RSS itself is basically a creation of well formed XML file in a pre defined format.
The basic main RSS tags are shown below
<rss version="2.0">
<channel>
......
</channel>
</rss>
<RSS> tag is the global container of the xml element, It is the root element of the RSS Feeds.
<CHANNEL >tag contains one or many description tags such as <title>, <description>, <link> and mainly <item> tag which is very important. You can have as many items tags on your RSS Feeds. Please check below code snippet for very basic RSS feeds
<rss version="2.0">
<channel>
<title>Worldofasp.net</title>
<link>http://www.worldofasp.net/</link>
<description>Article and Directory in ASP.NET</description>
<item>
<title>First Article</title>
<link>http://www.yourwebsite.com/createrss.html</link>
<description>... some text... </description>
<pubDate>02/15/2005</pubDate>
<author>David Smith</author>
</item>
<item>
<title>Second Article</title>
<link>http://www.yourwebsite.com/createrss.html</link>
<description>... some text... </description>
<pubDate>02/16/2005</pubDate>
<author>Michael Sullivan</author>
</item>
</channel>
</rss>
If you see the xml code above, it is quite self explanatory.
The above tag details <title> provides the title of the channel or the website.
<link> provides the url of the website that provided the RSS.
<description> provides some content from the RSS provider.
<item> very important tag to keep in mind, every RSS must contain atleast one <item> tag to provide content.
<pubDate> contains the date of the item
<author> contains author of the item
Using ASP.NET to generate the RSS Feeds
The whole idea of RSS Feeds is to generate an XML File with the format as I explained above. So, lets start coding this by creating new aspx file in your Visual Studio. Named the File as Rss.aspx
and add the following code in the Page_Load Event
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter xtw = new XmlTextWriter(Response.OutputStream,Encoding.UTF8);
xtw.WriteStartDocument();
xtw.WriteStartElement("rss");
xtw.WriteAttributeString("version", "2.0");
xtw.WriteStartElement("channel");
xtw.WriteElementString("title", "Latest Articles List");
xtw.WriteElementString("link", "http://www.worldofasp.net");
xtw.WriteElementString("description", "some description here");string sql = "Select * from tblArticle Order by DateCreated Desc";
SqlDataAdapter da = new SqlDataAdapter(sql, @"Data Source=.\SQLEXPRESS;AttachDbFilename="
+ Server.MapPath(@"App_Data\Database.mdf")+ ";Integrated Security=True;User Instance=True");
DataTable dt = new DataTable();
da.Fill(dt);if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
xtw.WriteStartElement("item");
xtw.WriteElementString("title", dt.Rows[i]["Title"].ToString());
xtw.WriteElementString("description", dt.Rows[i]["Description"].ToString());
xtw.WriteElementString("link", "http://www.yourwebsite.com/" + dt.Rows[i]["URL"].ToString());
xtw.WriteElementString("pubDate", dt.Rows[i]["DateCreated"].ToString());
xtw.WriteEndElement();
}
}
xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Flush();
xtw.Close();
}
If you see from the code above, it is quite simple and self explanatory. We do a loop from a database and then form the xml according to RSS format specification.
We grab the RSS data from the the table called tblArticle. We get the latest article and sort by the date. You can modify the code above, so that it only gets the last 10 article from your database. So your RSS feed publisher will always get the latest and updated article from your site. Thats all you need to build a RSS feed for your website.
Download Source Code