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:
In the code-behind for the .aspx page, just build your RSS data and output it with Response.Write.
<%@ OutputCache Duration="60" VaryByParam="none" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="rss.aspx.cs" Inherits="rss" %>
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 Pagetitle>
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 Pagetitle>
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 =
@"
- ";
{0}
{1}
{2}
{3}
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 =
@""2.0""> ";
xyz
http://aspdotnetcodebook.blogspot.com
by xyz
{1}
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:
Post a Comment