Beanie posted on April 14, 2010 11:51

For a while I had wanted to add RSS feed for my news sections of my websites, thinking that it had to be relativly straight forward, given it's only XML, I went looking for a simple way to impliment it.

This is how I ened up creating my RSS feeds.

The most important bit is this line in the head section of the page:

<link rel="alternate" href="/feed/" title="website RSS Feed" type="application/rss+xml" />

This tells the browser where the feed is and displays the RSS icon in the address bar of Firefox etc.

The we need to conect to a data source, this is in the web.config file:

<connectionStrings><add name="ConnectionString" connectionString="Data Source=server;Initial Catalog=database;User ID=user;Password=password"providerName="System.Data.SqlClient" /></connectionStrings>

Finally we are ready to create the XML document that will become the RSS feed, using the XmlTextWriter.

   1:  using System;
   2:  using System.Xml;
   3:  using System.Text;
   4:  using System.Collections.Generic;
   5:  using System.Linq;
   6:  using System.Web;
   7:  using System.Web.UI;
   8:  using System.Web.UI.WebControls;
   9:  using System.Data.SqlClient;
  10:  using System.Configuration;
  11:  using System.Data;
  12:   
  13:  public partial class feed_default : System.Web.UI.Page
  14:  {
  15:      protected void Page_Load(object sender, EventArgs e)
  16:      {
  17:          // Clear any previous output from the buffer
  18:          Response.Clear();
  19:          Response.ContentType = "text/xml";
  20:          XmlTextWriter feedWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
  21:          feedWriter.WriteStartDocument();
  22:   
  23:          // These are RSS Tags
  24:          feedWriter.WriteStartElement("rss");
  25:          feedWriter.WriteAttributeString("version", "2.0");
  26:          feedWriter.WriteStartElement("channel");
  27:          feedWriter.WriteElementString("title", "website");
  28:          feedWriter.WriteElementString("link", "http://www.website.com");
  29:          feedWriter.WriteElementString("description", "website News Feed");
  30:          feedWriter.WriteElementString("copyright", "Copyright 2010 website. All rights reserved.");
  31:   
  32:          // Get list of for RSS        
  33:          SqlConnection conn = null;
  34:          conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  35:          SqlDataReader rdr = null;
  36:          conn.Open();
  37:          SqlCommand cmd = new SqlCommand("selectRssItems", conn);
  38:          cmd.CommandType = CommandType.StoredProcedure;
  39:          // optional search paramters (remove if not required)
  40:          cmd.Parameters.Add(new SqlParameter("@Parameter", "Search"));
  41:          rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  42:          // Write all Posts in the rss feed
  43:          while (rdr.Read())
  44:          {
  45:              feedWriter.WriteStartElement("item");
  46:              feedWriter.WriteElementString("title", rdr["title"].ToString());
  47:              feedWriter.WriteElementString("description", rdr["description"].ToString());
  48:              feedWriter.WriteElementString("link", "http://www.website.com/news.asp?news=" + rdr["linkID"].ToString());
  49:              feedWriter.WriteElementString("pubDate", rdr["Date"].ToString());
  50:              feedWriter.WriteEndElement();
  51:   
  52:          }         
  53:          // Close all open tags tags
  54:          feedWriter.WriteEndElement();
  55:          feedWriter.WriteEndElement();
  56:          feedWriter.WriteEndDocument();
  57:          feedWriter.Flush();
  58:          feedWriter.Close();
  59:          Response.End();
  60:      }
  61:  }

Posted in: Development - ASP  Tags: , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Page List

Search Blog

Tag Cloud

Recent Comments

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Beanie