<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<script runat="server" language="C#">
private void Page_Load(object sender, System.EventArgs e)
{
// Set the content-type
Response.ContentType = "text/xml";
Response.ContentEncoding = Encoding.UTF8;
// check to see if a cached version exists
if (Cache["RssFeed"] == null)
{
// build up the cache dynamically
DataTable articleData = CreateDataSource();
// Use an XmlTextWriter to write the XML data to a string...
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
// write out <rss version="2.0">
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
// write out <channel>
writer.WriteStartElement("channel");
// write out <channel>-level elements
writer.WriteElementString("title", "Example RSS Feed Title");
writer.WriteElementString("link", "http://myWebSite.com/");
writer.WriteElementString("description", "This is a demonstration of creating an RSS feed.");
writer.WriteElementString("ttl", "60");
// write out an <item> element for each of the first X articles
const int RSS_ITEMS = 10;
for (int i = 0; i < RSS_ITEMS; i++)
{
// write out <item>
writer.WriteStartElement("item");
// write out <item>-level information
writer.WriteElementString("title", articleData.Rows[i]["title"].ToString());
writer.WriteElementString("link", String.Format("http://myWebSite.com/article.aspx?id={0}", articleData.Rows[i]["articleID"]));
writer.WriteElementString("description", articleData.Rows[i]["synopsis"].ToString());
writer.WriteElementString("author", articleData.Rows[i]["author"].ToString());
// use DateTimeFormatInfo "r" to use RFC 1123 date formatting (same as RFC 822)
writer.WriteElementString("pubDate", ((DateTime) articleData.Rows[i]["dateAdded"]).ToString("r"));
// write out </item>
writer.WriteEndElement();
}
// write out </channel>
writer.WriteEndElement();
// write out </rss>
writer.WriteEndElement();
// save the string in the cache (cache for 1.5 hours)
Cache.Insert("RssFeed", sw.ToString(), null, DateTime.Now.AddHours(1.5), TimeSpan.Zero);
writer.Close();
}
// write out the cached value
Response.Write(Cache["RssFeed"].ToString());
}
private DataTable CreateDataSource()
{
DataTable articles = new DataTable();
// create the columns of the data source
articles.Columns.Add(new DataColumn("articleID", typeof(int)));
articles.Columns.Add(new DataColumn("title", typeof(string)));
articles.Columns.Add(new DataColumn("author", typeof(string)));
articles.Columns.Add(new DataColumn("dateAdded", typeof(DateTime)));
articles.Columns.Add(new DataColumn("synopsis", typeof(string)));
// add some articles
Random rndNum = new Random();
for (int i = 1; i < 15; i++)
{
DataRow newRow = articles.NewRow();
newRow["articleID"] = i;
newRow["title"] = "Article Number " + i.ToString();
switch (rndNum.Next(3))
{
case 0:
newRow["author"] = "Scott Mitchell";
break;
case 1:
newRow["author"] = "Jisun Lee";
break;
case 2:
newRow["author"] = "Dave Yates";
break;
}
newRow["dateAdded"] = DateTime.Now.Date.AddDays(-i);
newRow["synopsis"] = "This is where the description for the article would go!";
articles.Rows.Add(newRow);
}
return articles;
}
</script>