If you are a regular visitor of blogs, contents publishing or news related websites you must have noticed a small RSS or XML icon somewhere on the page. This is very useful technique which content presenters or authors used fairly regularly on blogs to publish their articles or tutorials to other websites who subscribed to their feeds. In this article I will show you how to generate RSS feed for your ASP.NET websites.
RSS (Really Simple Syndication) feed is an XML document that follows the specification used for syndication. In simple words, a web feed is a file that contains frequently updated information, such as news headlines, blog posts, audio and video podscasts that is specially formatted in a way that allows it to be subscribed to using a variety of programs called RSS readers, feed readers or aggregators.
You can see one example of such RSS feed Icon on the left hand side of this page which you can use to subscribe to latest tutorials I publish on this website. To put RSS Feed Icon on your website you can simple use ASP.NET Hyperlink control which has NavigateUrl property set as follows:
<asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="images/button_rss.jpg"
NavigateUrl="RssFeed.aspx">RSS Feed</asp:HyperLink>
Hyperlink control above has NavigateUrl property set to RssFeed.aspx file which is a normal ASP.NET page and that will be used to generate RSS feed. Before we start generating XML document I want to show you how RSS Feed looks like. Have a look on the following XML showing basic information about the channel and one item. RSS Feed normally generates many items similar to <item> element in the following sample code.
Sample RSS Feed
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>EzzyLearning.com</title>
<link></link>
<description>Latest Tutorials on VB.NET, ASP.NET, C#, etc.</description>
<copyright>Copyright 2008-2009 EzzyLearning.com</copyright>
<item>
<title>Using Checkbox in ASP.NET GridView Control</title>
<description>This tutorial will show you how to use CheckBox inside GridView… </description>
<link>/tutorial.aspx?tid=5187857</link>
<pubDate>Wed, 18 Feb 2009 22:22:05 GMT</pubDate>
</item>
</rss>
Let’s start our RssFeed.aspx code where all the magic is happening. I am going to give you step by step coding of this file so that you can learn and implement it very quickly for your projects.
Step 1: Import the following .NET Namespaces at the top of your file.
using System.Data.SqlClient;
using System.Xml;
using System.Text;
Step 2: Write some ADO.NET code to get Latest news or tutorials from the SQL Server Database.
string constr = @"Your Database Connect String Here";
string query = @"SELECT Top 10 TutorialID, Heading, Description, PostedDate FROM TutorialTable ORDER BY PostedDate DESC";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
For simplicity I am using ADO.NET objects directly but you can use any technique you want or can also use your Data Access Layer that returns latest tutorials to you for publishing. I am also storing connection string directly in the code but it is highly recommended to store connection strings in web.config.
Step 3: Start Generating XML Document using .NET XmlTextWriter object available in System.Xml namespace.
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteStartElement("channel");
writer.WriteElementString("title", "EzzyLearning.com");
writer.WriteElementString("link", "/");
writer.WriteElementString("description", "Latest Tutorials on VB.NET, ASP.NET, C#, etc.");
writer.WriteElementString("copyright", "Copyright 2008-2009 EzzyLearning.com");
Step 4: Generate <item> XML element for every single tutorial or news item you want to publish.
foreach (DataRow row in table.Rows)
{
string id = row["TutorialID"].ToString();
string title = row["Heading"].ToString();
string description = row["Description"].ToString();
string link = "/tutorial.aspx?tid=" + id;
DateTime postedDate = Convert.ToDateTime(row["PostedDate"]);
writer.WriteStartElement("item");
writer.WriteElementString("title", title);
writer.WriteElementString("description", description);
writer.WriteElementString("link", link);
writer.WriteElementString("pubDate", postedDate.ToString("r"));
writer.WriteEndElement();
}
In the above code I am simply iterating all the rows in DataTable and after retrieving data from the rows in local variables I am writing XML Elements using WriteElementString method of XmlTextWriter class.
Step 5: Complete your XML Document and Flush and close your writer object.
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Step 6: Set the Response content type to XML and End your response.
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "text/xml";
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.End();
nice Article Waqas =)