using System;
using System.Data;
using System.ServiceModel.Syndication;
using System.Web;
using System.Collections.Generic;
using System.Xml;
namespace RSS
{
public partial class rss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
// I don't want just anyone to subscribe, so you have to know the GUID.
if (id== null || id != "23F14EA1-1B20-443B-9B94-92C4EA4A8099")
throw new Exception("Guid not reconized");
Response.ContentType = "application/atom+xml";
// this gets the data from the database and populates a table.
DataTable dt = cDB.getFeed();
SyndicationFeed myFeed = new SyndicationFeed();
myFeed.Title = TextSyndicationContent.CreatePlaintextContent("SampleApp Activity");
myFeed.Description = TextSyndicationContent
.CreatePlaintextContent(@"A syndication of the most recently
SampleApp activity including exceptions.");
myFeed.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/rss.aspx"))));
myFeed.Links.Add(SyndicationLink.CreateSelfLink(
new Uri(GetFullyQualifiedUrl(Request.RawUrl))));
myFeed.Copyright = TextSyndicationContent
.CreatePlaintextContent("Copyright SampleApp");
myFeed.Language = "en-us";
List<SyndicationItem> feedItems = new List<SyndicationItem>();
foreach (DataRow dr in dt.Rows)
{
SyndicationItem item = new SyndicationItem();
item.Title = TextSyndicationContent.CreatePlaintextContent(dr["title"].ToString());
SyndicationPerson authInfo = new SyndicationPerson();
authInfo.Email = "SampleApp@YourDomain.com";
item.Authors.Add(authInfo);
// RSS feeds can only have one author.
// The stored proc returns different categories of data that I am interested in.
switch (dr["category"].ToString())
{
case "WindFarms":
case "WindFarms ":
item.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/WindFarms.aspx"))));
authInfo.Name = "SampleApp WindFarm";
break;
case "Exceptions":
item.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/ErrorLog.aspx"))));
authInfo.Name = "SampleApp Exception";
break;
default:
authInfo.Name = "SampleApp";
break;
}
item.Summary = TextSyndicationContent.CreatePlaintextContent(
dr["summary"].ToString());
item.Categories.Add(new SyndicationCategory(dr["category"].ToString()));
item.PublishDate = DateTime.Parse(dr["pubdate"].ToString());
item.LastUpdatedTime = item.PublishDate;
item.Id = item.PublishDate.ToString();
// Add the item to the feed
feedItems.Add(item);
}
myFeed.Items = feedItems;
XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream);
// Use Atom 1.0
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(myFeed);
atomFormatter.WriteTo(feedWriter);
feedWriter.Close();
}
private string GetFullyQualifiedUrl(string s)
{
Uri u = new Uri(HttpContext.Current.Request.Url, s);
return u.ToString();
}
}
}