Difficulty: Easy (Less than 10 minutes)
In this page you can understand, in layman's terms, how we go and get some news from our database and render it on the screen in an XML (RSS) format. We are using C# in this example.
Understand The XML (RSS) First!
title
This is the name of the channel
link
The link to your website
description
You can add a description to describe what this feed is all about.
ttl
This is the total number of minutes your feed can be cached before refreshing from the source. (i.e. your database)
We then move on to the individual items in your XML feed.
title
The name of the article (e.g. My new website has been launched)
link
The URL link to this partcular item on your website (e.g. http://www.mywebsite.com/myblog/my-new-website.html)
description
Add a more detailed overview of this item (e.g. We are pleased to launch our new website - it has some great features including colour and videos)
guide
You will, more often that not, have an individual page for your article (e.g. http://www.mywebsite.com/myblog/?id=23 ) * Note: we never use this and just use "link" above
pubDate
This will show the date when the article was published.
There are a number of other elements you can add. You can visit http://cyber.law.harvard.edu/rss/rss.html for more details.
That is about it for what is rendered to the screen, now we will take a look at how we are going to complete this task.
Step 1
Create yourself a new page and let's call it myrsspage.aspx
Step 2
Skip past the front page, go straight to the page behind.
Step 3
Add this to the declaration at the top of the page:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Xml;
We are not going to go into what these items do in this example.
Step 4
Look for the Page Load Event (This is what is going to happen when the page loads each time)
Step 5
Let's add some code to the Page Load Event:
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", "My New RSS Feed");
objX.WriteElementString("link", "http://www.myblog.com/");
objX.WriteElementString("description", "The very latest news from my website");
objX.WriteElementString("ttl", "30");SqlConnection objCon = new SqlConnection(ConfigurationSettings.AppSettings["sqlstr"]);
objCon.Open();
string sql = "SELECT title, desc, id, submitted FROM mytable ORDER BY submitted 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.myblog.com/blog/?id=" +
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();
This is a pretty quick way to do it and suggest, for best practice, using a stored procedure to query your database rather than the inline sql we have used.
Have fun!