RSS has lots of features, however implementing it is dead easy. So easy in fact, that it took less than 10 minutes to add it to CodeBlog, and I had never worked with RSS before.
The concept behind RSS is to provide a link that feed readers can access to get the latest content from a dynamic site.
The RSS file is an XML document, with a minimalistic schema:
1 <rss version="2.0">
2 <channel>
3 <title>Website Feed Title</title>
4 <link>Website Feed URL</link>
5 <description>Website Feed description</description>
6 <ttl>Time in Minutes to Refresh Cache</ttl>
7 <item>
8 <title>Post Subject/Title</title>
9 <description>Post Description / Full Text of Post</description>
10 <link>Permalink to Post</link>
11 <pubDate>Date Published</pubDate>
12 </item>
13 </channel>
14 </rss>
Of course, RSS has much more to it, but this all that is required to syndicate blog posts. For more information, take a look at the full specification.
To implement this in CodeBlog, I created a ASPX page called RSSFeed.aspx and removed all content except the Page directive.
I used the Page_Load event in the code behind to generate the XML:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 Response.Clear();
4 Response.ContentType = "text/xml";
5 XmlTextWriter rssXml = new XmlTextWriter(Response.OutputStream,Encoding.UTF8);
6
7 rssXml.WriteStartDocument();
8
9 rssXml.WriteStartElement("rss");
10 rssXml.WriteAttributeString("version", "2.0");
11 rssXml.WriteStartElement("channel");
12
13 rssXml.WriteElementString("title", CodeBlog.Configuration.ConfigurationManager.BlogTitle);
14 rssXml.WriteElementString("link", CodeBlog.Configuration.ConfigurationManager.BaseDomain);
15 rssXml.WriteElementString("description", CodeBlog.Configuration.ConfigurationManager.BlogDescription);
16 rssXml.WriteElementString("ttl", "60");
17
18 PostCollection posts;
19
20 posts = PostService.GetMostRecentPosts(10);
21
22 foreach (Post post in posts)
23 {
24 rssXml.WriteStartElement("item");
25 rssXml.WriteElementString("title", post.Subject);
26 rssXml.WriteElementString("description", post.Text);
27 rssXml.WriteElementString("link", PostService.GetPostURL(post.PostID));
28 rssXml.WriteElementString("pubDate", post.CreatedOn.ToShortDateString());
29 rssXml.WriteEndElement();
30 }
31
32 rssXml.WriteEndElement();
33 rssXml.WriteEndElement();
34 rssXml.WriteEndDocument();
35 rssXml.Flush();
36 rssXml.Close();
37 Response.End();
38 }
The .NET framework has excellent built in XML tools so there is no need to worry about outputting valid XML, the framework does it for you.
The foreach loop creates an <item> element for each blog post returned by the business object.
Overall, RSS has been the easiest feature I have added to CodeBlog to date. The actual blog engine will be available in open source form for download next week.
0 comments:
Post a Comment