1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Xml;
5:
6: namespace Site
7: {
8: /// <summary>
9: /// WebPage: RSSFeed.aspx
10: /// Class: RSSFeed
11: /// Creator: James Craig
12: /// Description: Implements a simple RSS feed.
13: /// </summary>
14: public partial class RSSFeed : System.Web.UI.Page
15: {
16: /// <summary>
17: /// Called at page loading
18: /// </summary>
19: /// <param name="sender"></param>
20: /// <param name="e"></param>
21: protected void Page_Load(object sender, EventArgs e)
22: {
23: //Now make the response an xml file
24: Response.Clear();
25: Response.ContentType = "text/xml";
26: XmlTextWriter RSSOutput = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
27: //initialize the xml document for RSS using version 2.0
28: RSSOutput.WriteStartDocument();
29: RSSOutput.WriteStartElement("rss");
30: RSSOutput.WriteAttributeString("version", "2.0");
31: LoadItems(RSSOutput);
32:
33: //Close the document
34: RSSOutput.WriteEndElement();
35: RSSOutput.WriteEndDocument();
36: RSSOutput.Flush();
37: RSSOutput.Close();
38: }
39:
40: /// <summary>
41: /// Loads Items and adds them to the feed.
42: /// </summary>
43: /// <param name="RSSOutput"></param>
44: private void LoadItems(XmlTextWriter RSSOutput)
45: {
46: //Load the rss items (database call, etc. would go here)
47:
48: //Create a default channel
49: RSSOutput.WriteStartElement("channel");
50: RSSOutput.WriteElementString("title", "Channel Title");
51: RSSOutput.WriteElementString("link", "Your website");
52: RSSOutput.WriteElementString("description", "Description of the channel");
53: RSSOutput.WriteElementString("copyright", "Copyright " + DateTime.Now.Year.ToString() + ". All rights reserved.");
54:
55: //Now output each of the announcements.
56: foreach (Item in your list of items)
57: {
58: RSSOutput.WriteStartElement("item");
59: RSSOutput.WriteElementString("title", "Item's Title");
60: RSSOutput.WriteElementString("description", "Actual text of the item");
61: RSSOutput.WriteElementString("pubDate", "Item's date (use ToString("R") for the date output)";
62: RSSOutput.WriteElementString("link", "Link to the item");
63: RSSOutput.WriteEndElement();
64: }
65: //Close everything up.
66: RSSOutput.WriteEndElement();
67: }
68: }
69: }