RSS.ashx
<%@ WebHandler Language="C#" Class="Rss" %>
using System;
using System.Web;
using System.Data;
using System.Text;
using System.Xml;
using DataAccess;
public class Rss : IHttpHandler {
ArticleDAO articleDAO = new ArticleDAO();
public void ProcessRequest (HttpContext context) {
// Clear any previous output from the buffer
context.Response.Clear();
context.Response.ContentType = "text/xml";
XmlTextWriter cdRSS = new
XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
cdRSS.WriteStartDocument();
cdRSS.WriteStartElement("rss");
cdRSS.WriteAttributeString("version",
"2.0");
cdRSS.WriteStartElement("channel");
cdRSS.WriteElementString("title", "CodeDigest.com
Latest Articles");
cdRSS.WriteElementString("link",
"http://www.codedigest.com");
cdRSS.WriteElementString("description", "Latest
articles hosted on CodeDigest.com.");
cdRSS.WriteElementString("copyright", "Copyright
2008 - 2009 CodeDigest.com. All rights reserved.");
//Connect database to get the data
DataTable dtArticles =
articleDAO.GetArticlesForRss();
for (int i = 0; i < dtArticles.Rows.Count;
i++)
{
//Build Item tags with the data from database
cdRSS.WriteStartElement("item");
cdRSS.WriteElementString("title",
dtArticles.Rows[i]["Title"].ToString());
cdRSS.WriteElementString("description",
dtArticles.Rows[i]["description"].ToString());
cdRSS.WriteElementString("link", "http://"+
context.Request.Url.Host + dtArticles.Rows[i]["URL"].ToString());
cdRSS.WriteElementString("pubDate",
dtArticles.Rows[i]["ApprovedOn"].ToString());
cdRSS.WriteEndElement();
}
cdRSS.WriteEndElement();
cdRSS.WriteEndElement();
cdRSS.WriteEndDocument();
cdRSS.Flush();
cdRSS.Close();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
You can read more about HttpHandler from the articles in
the reference section.
|