Well, I’ve finally gotten around to publishing an RSS feed for the articles on Developer Fusion . You can get it here – http://www.developerfusion.com/rss/contentrss.aspx?type=articles&language=all .
For anyone who’s interested in the code behind it – I just wrote this simple helper class.
/// <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.WriteComment(“Generated by Developer Fusion RSS feed at ” + DateTime.Now.ToString(“r”));
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 )
{
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”,”James Crowley”);
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! 🙂
// 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”);
// generate the items here
gen.WriteItem(“Some Great Article,”http://www.developerfusion.com/show/10/”,”description”,”James Crowley”,DateTime.Now, “ASP.NET”);
// clear up
gen.WriteEndChannel();
gen.WriteEndDocument();
gen.Close();
Oh – and if anyone can show me how to post this code correctly on these weblogs, then please drop me a line!
Leave a Reply to Juan Carlos Diaz Cancel reply