Create RSS feeds on your Website using ASP.NET

July 23, 2009

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Text;
using System.ServiceModel.Syndication;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class RSS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();

Response.ContentType = "text/xml";
XmlTextWriter rssFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
rssFeed.WriteStartDocument();
rssFeed.WriteStartElement("rss");
rssFeed.WriteAttributeString("version", "2.0");
rssFeed.WriteStartElement("channel");
rssFeed.WriteElementString("title", "University News");
rssFeed.WriteElementString("link", HttpContext.Current.Request.Path);
//a file with ".rss" extension, in root directory, is required to recognize the request as a feed
rssFeed.WriteElementString("link", "http://www.google.com/feed.rss");
rssFeed.WriteElementString("description", "The latest university news and information.");
rssFeed.WriteElementString("copyright", "Copyright 2009 techelp.in. All rights reserved.");

SqlConnection SqlCon;
SqlCommand SqlCom;
SqlDataReader SqlDR;

SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCom = new SqlCommand("select * from dbo.tblUniversityList", SqlCon);
SqlCom.CommandType = CommandType.Text;

if (SqlCon.State == ConnectionState.Closed)
{
SqlCon.Open();
}

SqlDR = SqlCom.ExecuteReader();

while (SqlDR.Read())
{
rssFeed.WriteStartElement("item");
string Name = SqlDR["University"].ToString();
rssFeed.WriteElementString("title", Name);
rssFeed.WriteElementString("description", SqlDR["University"].ToString());
rssFeed.WriteElementString("link", HttpContext.Current.Request.Path + SqlDR["ID"]);
rssFeed.WriteEndElement();
}

SqlDR.Close();
SqlCon.Close();

rssFeed.WriteEndElement();
rssFeed.WriteEndElement();
rssFeed.WriteEndDocument();

rssFeed.Flush();
rssFeed.Close();
Response.End();

}
}
Share and Enjoy:
  • Twitter
  • Google Buzz
  • DZone
  • Digg
  • Reddit
  • Diigo
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • email
  • PDF
  • Add to favorites

tags: ,
posted in ASP.NET by varun

Follow comments via the RSS Feed | Leave a comment | Trackback URL

2 Comments to "Create RSS feeds on your Website using ASP.NET"

  1. Roulette System wrote:

    I cannot believe this is true!

    [Reply]

  2. Roulette Marginale wrote:

    Hey, ok, I get it, I guess – but does this really work?

    [Reply]

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org