|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
BackgroundWith all of the new hype surrounding RSS and weblogs, I decided to create a simple RSS feed for my site. Along the way I ran into a couple excentricities that I thought I would share with my fellow CP'ians. If you would like to see this code in action, check out. I started out by locating a RSS 2.0 specification, which can be found here. I won't go
into an explanation of the spec, because they do a better job themselves than I
could. Once I had reviewed that, I went out looking for an existing example. I
found a few, but they all used a ImplementationOnce I had decided to use the public XmlTextWriter WriteRSSPrologue(XmlTextWriter writer)
{
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");
writer.WriteAttributeString("xmlns:blogChannel",
"http://backend.userland.com/blogChannelModule");
writer.WriteStartElement("channel");
writer.WriteElementString("title","Simple RSS Document");
writer.WriteElementString("link","http://www.danielbright.net/");
writer.WriteElementString("description",
"A simple RSS document generated using XMLTextWriter");
writer.WriteElementString("copyright","Copyright 2002-2003 Dan Bright");
writer.WriteElementString("generator","RSSviaXmlTextWriter v1.0");
return writer;
}
As you can see, you pass this method the Now we move on to adding "All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred)." This had me stumped. I spent about 20 minutes trying to roll my own public XmlTextWriter AddRSSItem(XmlTextWriter writer,
string sItemTitle, string sItemLink,
string sItemDescription)
{
writer.WriteStartElement("item");
writer.WriteElementString("title",sItemTitle);
writer.WriteElementString("link",sItemLink);
writer.WriteElementString("description",sItemDescription);
writer.WriteElementString("pubDate", DateTime.Now.ToString("r"));
writer.WriteEndElement();
return writer;
}
This time we pass the method the Note: It will generate the Now we have to cleanup behind our RSS document, again passing the public XmlTextWriter WriteRSSClosing(XmlTextWriter writer)
{
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
return writer;
}
Once we have this foundation in place, we move on to actually creating a RSS document. This is where I ran into my second problem. I had been using a Another Google led me to a post
on Roy Osherove's weblog discussing this very thing. A big thanks goes out to
Stephane, who pointed out that a private void Page_Load(object sender, System.EventArgs e)
{
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,
System.Text.Encoding.UTF8);
WriteRSSPrologue(writer);
AddRSSItem(writer,"Item Title","http://test.com", "This is a test item");
AddRSSItem(writer,"Item 2 Title", "http://test.com/blabla.aspx">http://test.com/blabla.aspx,
"This is the second test item");
AddRSSItem(writer,"<b>Item 2 Title</b>", "http://test.com/blabla.aspx">http://test.com/blabla.aspx,
"This is the second test item");
WriteRSSClosing(writer);
writer.Flush();
writer.Close();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.End();
}
And there we have it. This will now output valid RSS, even
when invalid characters are passed thanks to the History
|
||||||||||||||||||||||