How to add an RSS feed in website
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?
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 dummy Data Method 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():
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 a tag to the section of your .aspx pages. For example, here's my tag:
<head runat="server"> <link rel="alternate" type="application/rss+xml" title="RSS" href="rss.aspx" runat="server" id="rss_link" visible="false" /> <title>Untitled Page</title> </head>
check out this complete code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Rss.aspx.cs" Inherits="Rss" %> <%@ OutputCache Duration="60" VaryByParam="none" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <link rel="alternate" type="application/rss+xml" title="RSS" href="rss.aspx" runat="server" id="rss_link" visible="false" /> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Rss : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { rss_link.Visible = true; if (!IsPostBack) { DataTable entriesDataSet = new DataTable(); entriesDataSet = Data(); 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.Rows) { string url = string.Format("http://localhost/MyPractice/Rss.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>xyz</title> <link>http://aspdotnetcodebook.blogspot.com</link> <description>by xyz</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); Response.End(); } } public DataTable Data() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Title", typeof(string)); dt.Columns.Add("link", typeof(string)); dt.Columns.Add("description", typeof(string)); dt.Columns.Add("TimeStamp", typeof(DateTime)); dt.Rows.Add(new object[] { "1", "test", "http://abc.com ", "test", "12/12/2008" }); dt.Rows.Add(new object[] { "2", "test", "http://abc.com ", "test", "12/12/2008" }); dt.Rows.Add(new object[] { "3", "test", "http://abc.com ", "test", "12/12/2008" }); return dt; } }
0 comments: