using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Web.UI;
namespace RSS
{
#region RSSItem class
///
/// An RSS feed item.
///
public class RSSItem
{
private string title;
///
/// Gets or sets the title for the item.
///
public string Title
{
get { return title; }
set { title = value; }
}
private string description;
///
/// Gets or sets the description for the item.
///
public string Description
{
get { return description; }
set { description = value; }
}
private DateTime date;
///
/// Gets or sets the date of the item.
///
public DateTime Date
{
get { return date; }
set { date = value; }
}
private string author;
///
/// Gets or sets the author of the item.
///
public string Author
{
get { return author; }
set { author = value; }
}
private string link;
///
/// Gets or sets the URL for the item.
///
public string Link
{
get { return link; }
set { link = value; }
}
}
#endregion
#region RSSItemComparer class
internal class RSSItemComparer : IComparer
{
public int Compare(object x, object y)
{
RSSItem item1 = x as RSSItem;
RSSItem item2 = y as RSSItem;
return -(item1.Date.CompareTo(item2.Date));
}
}
#endregion
#region RSSGenerator class
///
/// Helper class to generate RSS feeds.
///
public class RSSGenerator : Page
{
private ArrayList items = new ArrayList();
///
/// Creates a new instance.
///
public RSSGenerator()
{
}
/// Category for RSS properties
protected const string RSSCategory = "RSS";
///
/// Event triggered when the page is loaded.
///
/// E.
protected override void OnLoad (EventArgs e)
{
Page.Response.Clear();
Page.Response.ContentType = "text/xml";
base.OnLoad(e);
string rss = RSSFeedXml();
Page.Response.Write(rss);
}
///
/// Adds an RSS item to the feed.
///
public RSSItem AddItem()
{
RSSItem item = new RSSItem();
items.Add(item);
return item;
}
///
/// Clears the RSS items from the feed.
///
public void ClearItems()
{
items.Clear();
}
#region properties
private string title;
///
/// Gets or sets the title of the feed.
///
[Description("The title of the RSS feed")]
[Category(RSSCategory)]
public string Title
{
get { return title; }
set { title = value; }
}
private string link;
///
/// Gets or sets the URL for the website associated with this feed.
///
[Description("The URL of the homepage for this feed")]
[Category(RSSCategory)]
public string Link
{
get { return link; }
set { link = value; }
}
private string description;
///
/// Gets or sets the description for this feed.
///
[Description("Description of the feed")]
[Category(RSSCategory)]
public string Description
{
get { return description; }
set { description = value; }
}
#endregion
///
/// Returns the XML for the RSS feed.
///
public string RSSFeedXml()
{
PopulateFeed();
StringWriter stringWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(stringWriter);
// start document
writer.WriteStartElement("rss");
writer.WriteAttributeString("version","2.0");
// channel
writer.WriteStartElement("channel");
writer.WriteElementString("title", title);
writer.WriteElementString("link", link);
writer.WriteElementString("description", description);
items.Sort(new RSSItemComparer());
foreach(RSSItem item in items)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", item.Title);
writer.WriteElementString("description", item.Description);
writer.WriteElementString("pubDate", item.Date.ToString("r"));
writer.WriteElementString("author", item.Author);
writer.WriteElementString("link", item.Link);
writer.WriteEndElement(); // item
}
// end channel
writer.WriteEndElement(); //channel
// end document
writer.WriteEndElement(); //rss
writer.Flush();
return stringWriter.ToString();
}
///
/// Populates the feed. Override to populate the feed
///
protected virtual void PopulateFeed()
{
}
}
#endregion
}