Ads by Lake Quincy Media
| Next Tip?
Home » Asp.net, Interview Questions

Generating RSS Feed : A simple way

27 October 2008 115 views One Comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Generating RSS Feed : A simple way
Around a week back one of my colleagues need to geenrate an RSS Feed for Site. This is my try towards the same.

Here, I have used database of my site and try to generate a RSS Feed for my Site.
Anyone can use the same with a little change.
1. As this will generate a RSS Feed so there is no need to do more with design page only besides few following lines
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RSSFeedGenerator.aspx.cs" Inherits="RSSFeedGenerator" EnableViewState="false"%>
<%@ OutputCache Duration="300" 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">
<title>Simple RSS Feed Generator</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>

2. In the code-behind page every thing need to be written as :
/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : <a href="http://www.msdotnetheaven.com">Gaurav Arora</a>*/
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Xml;

public partial class RSSFeedGenerator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
objX.WriteStartDocument();
objX.WriteStartElement("rss");
objX.WriteAttributeString("version","2.0");
objX.WriteStartElement("channel");
objX.WriteElementString("title", "A new Web Developer Installer Launched by MicroSoft");
objX.WriteElementString("link", "http://www.msdotnetheaven.com");
objX.WriteElementString("description", "I am very happy to read this news as a Web Developer that MicroSoft launched A free Web Platform installer and its free! It provides : 1. Install entire microSoft Web Platform 2.Quickly get started web application using MS Utilities like asp.net and MVC, silverlight etc. 3. Includes updated IIS with version 7.0 Its worth ...");
objX.WriteElementString("copyright","(c) 2008, MsDotNetHeaven News. All rights reserved.");
objX.WriteElementString("ttl","5");

SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["conString"]);
objCon.Open();

//Change your feed table and query
string sql = "SELECT TOP 10 Title, Summary, ArticleID, PostTime FROM Articles ORDER BY PostTime DESC";

SqlCommand objCmd = new SqlCommand(sql, objCon);
SqlDataReader objReader = objCmd.ExecuteReader();

while (objReader.Read())
{
objX.WriteStartElement("item");
objX.WriteElementString("title",objReader.GetString(0));
objX.WriteElementString("description",objReader.GetString(1));
objX.WriteElementString("link", "http://www.msdotnetheaven.com/?p=" + objReader.GetInt32(2).ToString());
objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"));
objX.WriteEndElement();
}
objReader.Close();
objCon.Close();

objX.WriteEndElement();
objX.WriteEndElement();
objX.WriteEndDocument();
objX.Flush();
objX.Close();
Response.End();
}
}

Following are the step(s):
1. Start Visual Studio
2. Create a new project
3. Add above two files
4. Press F5
There are some specification to follow when RSS Feed generates : <a href=http://cyber.law.harvard.edu/rss/rss.html>RSS Feed Specification</a>

The following is the feed document :
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;rss version="0.92"&gt;
&lt;channel&gt;
&lt;title&gt;MsDotNetHeaven&lt;/title&gt;
&lt;link&gt;http://www.msdotnetheaven.com&lt;/link&gt;
&lt;description&gt;Lets Share Knowledge...&lt;/description&gt;
&lt;lastBuildDate&gt;Sat, 18 Oct 2008 20:49:57 +0000&lt;/lastBuildDate&gt;
&lt;docs&gt;http://backend.userland.com/rss092&lt;/docs&gt;
&lt;language&gt;en&lt;/language&gt;

&lt;item&gt;
&lt;title&gt;A new Web Developer Installer Launched by MicroSoft&lt;/title&gt;
&lt;description&gt;I am very happy to read this news as a Web Developer that MicroSoft launched A free Web Platform installer and its free!

It provides :
1. Install entire microSoft Web Platform
2.Quickly get started web application using MS Utilities like asp.net and MVC, silverlight etc.
3. Includes updated IIS with version 7.0

Its worth ...&lt;/description&gt;
&lt;link&gt;http://www.msdotnetheaven.com/?p=106&lt;/link&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;Embedding &#8220;Windows Media Player&#8221; into a web page&lt;/title&gt;
&lt;description&gt;Hey Friends!!!

Would you like to know, how can we integrate Windows "Media Player" through ASP.NET?

Isn't it pretty interesting? If yes, then please go through my post below:

You probably saw many websites that include an embeded video player in their pages. Most of the time, the embedded video player is ...&lt;/description&gt;
&lt;link&gt;http://www.msdotnetheaven.com/?p=37&lt;/link&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;Adobe new release : Flash Player 10&lt;/title&gt;
&lt;description&gt;On Wednesday Adobe shipped its new Flash Player version 10 with main feratures:
1. 3D capabilities build in feature
2. Developers can add their own special effects
3. Upgraded to rich internet technology

With the above release it is cleared that now Adobe featuring to web applications with folowing some extra features:
1. Online video
2. ...&lt;/description&gt;
&lt;link&gt;http://www.msdotnetheaven.com/?p=34&lt;/link&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;Silverlight2.0 : releases on Monday&lt;/title&gt;
&lt;description&gt;Get ready for Silverlight2.0 release on MondayI got the news from one of my friend and want to share the same with you. MicroSoft decided to release Silverlight2.0 on Monday.I have not more for its:1. Features2. Working3. New implementations4. Flexibility etc.Only the idea is that its a great inventions in ...&lt;/description&gt;
&lt;link&gt;http://www.msdotnetheaven.com/?p=104&lt;/link&gt;
&lt;/item&gt;
&lt;item&gt;
&lt;title&gt;Monday : A day for Silverlight2.0 release&lt;/title&gt;
&lt;description&gt;Get ready for Silverlight2.0 release on Monday

I got the news from one of my friend and want to share the same with you. MicroSoft decided to release Silverlight2.0 on Monday.

I have not more for its:
1. Features
2. Working
3. New implementations
4. Flexibility etc.
Only the idea is that its a great inventions in ...&lt;/description&gt;
&lt;link&gt;http://www.msdotnetheaven.com/?p=30&lt;/link&gt;
&lt;/item&gt;
&lt;/channel&gt;
&lt;/rss&gt;

Popularity: 1%

Post to Twitter Tweet This Post

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

One Comment »

  • ErvinTW said:

    Thanks! Nice post.

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes