When creating the RSS feed for Developer Fusion, I decided to create a simple helper class to generate the XML for me. An example using the class follows.
using System;
using System.Xml;
/// <summary>
/// Enables the generation of an RSS feed
/// </summary>
public class RSSFeedGenerator
{
XmlTextWriter writer;
public RSSFeedGenerator( System.IO.Stream stream, System.Text.Encoding encoding )
{
writer = new XmlTextWriter(stream, encoding);
writer.Formatting = Formatting.Indented;
}
public RSSFeedGenerator( System.IO.TextWriter w )
{
writer = new XmlTextWriter(w);
writer.Formatting = Formatting.Indented;
}
/// <summary>
/// Writes the beginning of the RSS document
/// </summary>
public void WriteStartDocument()
{
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");
}
/// <summary>
/// Writes the end of the RSS document
/// </summary>
public void WriteEndDocument()
{
writer.WriteEndElement(); //rss
writer.WriteEndDocument();
}
/// <summary>
/// Closes this stream and the underlying stream
/// </summary>
public void Close()
{
writer.Flush();
writer.Close();
}
/// <summary>
/// Begins a new channel in the RSS document
/// </summary>
/// <param name="title"></param>
/// <param name="link"></param>
/// <param name="description"></param>
public void WriteStartChannel(string title, string link, string description, string copyright, string webMaster)
{
writer.WriteStartElement("channel");
writer.WriteElementString("title",title);
writer.WriteElementString("link",link);
writer.WriteElementString("description",description);
writer.WriteElementString("language","en-gb");
writer.WriteElementString("copyright",copyright);
writer.WriteElementString("generator","Developer Fusion RSS Feed Generator v1.0");
writer.WriteElementString("webMaster",webMaster);
writer.WriteElementString("lastBuildDate",DateTime.Now.ToString("r"));
writer.WriteElementString("ttl","20");
}
/// <summary>
/// Writes the end of a channel in the RSS document
/// </summary>
public void WriteEndChannel()
{
writer.WriteEndElement(); //channel
}
/// <summary>
/// Writes an item to a channel in the RSS document
/// </summary>
/// <param name="title"></param>
/// <param name="link"></param>
/// <param name="description"></param>
/// <param name="author"></param>
/// <param name="publishedDate"></param>
/// <param name="category"></param>
public void WriteItem(string title, string link, string description, string author, DateTime publishedDate, string subject)
{
writer.WriteStartElement("item");
writer.WriteElementString("title",title);
writer.WriteElementString("link",link);
writer.WriteElementString("description",description);
writer.WriteElementString("author",author);
writer.WriteElementString("pubDate",publishedDate.ToString("r"));
//writer.WriteElementString("category",category);
writer.WriteElementString("subject",subject);
writer.WriteEndElement();
}
}
and then it was *really* simple to generate the RSS feed! :)
// set the content type
Page.Response.ContentType = "text/xml";
// create a RSS feed generator for the output
RSSFeedGenerator gen = new RSSFeedGenerator(Page.Response.Output);
gen.WriteStartDocument();
gen.WriteStartChannel("Developer Fusion RSS Feed",
"http://www.developerfusion.com/",
"Summary of the latest articles published on Developer Fusion",
"Copyright © Developer Fusion Ltd, 1999-2004", "James Crowley" );
// generate the items here
gen.WriteItem("Using ADO.NET with SQL Server",
"http://www.developerfusion.com/show/4278/",
"An extensive examination of using ADO.NET to access SQL Server databases, from establishing connections and executing stored procedures, to connection pools, data readers and data sets.",
"James Crowley", DateTime.Now, ".NET");
// clear up
gen.WriteEndChannel();
gen.WriteEndDocument();
gen.Close();
Comments