How to generate RSS feeds without any 3rd party components

RSS feeds are widely used in today’s internet world. Thus we, developers, have to implement it on our websites and projects most of the time. There are many free and paid components that generates RSS feeds but in fact you may generate it by yourself as well.

I will post a generic handler’s source code to show you how to do that.

Post is a class that has some basic properties like Url, Title, Teaser, etc. You may need to write your own class and implement these properties in a basic manner. Then you will need to create a GetPosts() method that returns a list of Post objects.

<%@ WebHandler Language="C#" Class="Rss" %>

using System;
using System.Web;
using System.Text;
using System.Xml;
using System.Collections.Generic;
using System.Linq;

public class Rss : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

        XmlTextWriter xmlWriter = new XmlTextWriter(context.Response.Output);

        xmlWriter.WriteStartElement("rss");
        xmlWriter.WriteAttributeString("version", "2.0");
        xmlWriter.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");

        xmlWriter.WriteStartElement("channel");

        #region Channel Details

        xmlWriter.WriteStartElement("title");
        xmlWriter.WriteCData("Sunali.com");
        xmlWriter.WriteEndElement(); // end title

        xmlWriter.WriteStartElement("link");
        xmlWriter.WriteCData("http://sunali.com");
        xmlWriter.WriteEndElement(); // end link

        xmlWriter.WriteStartElement("description");
        xmlWriter.WriteCData("Sunali.com Posts RSS Feed");
        xmlWriter.WriteEndElement(); // end description

        xmlWriter.WriteStartElement("language");
        xmlWriter.WriteString("tr-TR");
        xmlWriter.WriteEndElement(); // end language

        xmlWriter.WriteStartElement("pubDate");
        xmlWriter.WriteString(DateTime.Now.ToString("ddd, dd MMM yyyy hh:mm:ss zz"));
        xmlWriter.WriteEndElement(); // end pubDate
        #endregion

        // You are supposed to write your GetPosts method by yourself.
        List<Post> posts = GetPosts();

        foreach (Post post in posts)
        {
            xmlWriter.WriteStartElement("item");

            xmlWriter.WriteStartElement("title");
            xmlWriter.WriteCData(post.Title);
            xmlWriter.WriteEndElement(); // end title

            xmlWriter.WriteStartElement("link");
            xmlWriter.WriteCData(post.Url);
            xmlWriter.WriteEndElement(); // end link

            xmlWriter.WriteStartElement("description");
            xmlWriter.WriteCData(post.Teaser);
            xmlWriter.WriteEndElement(); // end description

            xmlWriter.WriteStartElement("dc:creator");
            xmlWriter.WriteCData(post.AuthorName);
            xmlWriter.WriteEndElement(); // end dc:creator

            xmlWriter.WriteStartElement("pubDate");
            xmlWriter.WriteString(post.PublishDate.ToString("ddd, dd MMM yyyy hh:mm:ss zz"));
            xmlWriter.WriteEndElement(); // end pubDate

            xmlWriter.WriteStartElement("guid");
            xmlWriter.WriteAttributeString("isPermaLink", "false");
            xmlWriter.WriteCData(post.Url);
            xmlWriter.WriteEndElement(); // end guid

            xmlWriter.WriteEndElement(); // end item
        }

        xmlWriter.WriteEndElement(); // end channel
        xmlWriter.WriteEndElement(); // end rss
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *