namespace MDL.RSSLibDLL
{
public class RssHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context) // this is the function that needs to be implemented. It comes, by dfault, with the Context of the user in the application that it handles
{
// Get the file name from the requested url
string fileName = Path.GetFileName(context.Request.Path); // the rss file that the user wants
//generally, you want to cache your rss files
string cachedChan = context.Cache[fileName] as String;
if (cachedChan == null)
{
// The file is not in the cache
string profileFolder = ConfigurationSettings.AppSettings["rssProfileFolder"];
//get the rss profile – the phyisical structure
if (profileFolder == null)
{
context.Response.StatusCode = 404;
context.Response.End();
return;
}
// Verify that you actually have a profile for the requested feed(a source rss file to read the structure from )
string profilePath = profileFolder + "" + fileName;
if (!File.Exists(profilePath))
{
context.Response.StatusCode = 404;
context.Response.End();
return;
}
// create the rss response
RssChannel chan = new RssChannel(profilePath, DateTime.Now);
cachedChan = chan.GetResponse();
double cacheTimeout;
string appCacheTimeout = ConfigurationSettings.AppSettings["cacheTimeout"];
if (appCacheTimeout == null)
cacheTimeout = 30;
else
cacheTimeout = double.Parse(appCacheTimeout);
context.Cache.Insert(fileName, cachedChan, null, DateTime.Now.AddMinutes(cacheTimeout), TimeSpan.Zero);
}
context.Response.ContentType = "text/xml";
context.Response.Write(cachedChan); // output the rss to the browser
}
public bool IsReusable
{
get
{
return true;
}
}
}
internal class RssChannel //my class that implements a Rss feed item
{
private string _title;
private string _link;
private string _description;
private string _language;
private string _date;
private string _profilePath; //resulting RSS structure
private ArrayList _items;
public RssChannel(string profilePath, DateTime date)
{
_profilePath = profilePath;
_date = date.Month.ToString() + "/" + date.Day.ToString();
Load();
}
public string Title
{
get { return (_title); }
set { _title = value; }
}
public string Link
{
get { return (_link); }
set { _link = value; }
}
public string Description
{
get { return (_description); }
set { _description = value; }
}
public string Language
{
get { return (_language); }
set { _language = value; }
}
public RssItem this[int index]
{
get
{
if (index < _items.Count)
return ((RssItem)_items[index]);
return (null);
}
}
public string GetResponse()
{
XmlElement workNode;
XmlElement itemNode;
RssItem currentItem;
XmlDocument rssDocument = new XmlDocument();
// Create the rss document node and set the its attributes
XmlElement docNode = rssDoc.CreateElement("rss");
XmlAttribute attr = rssDoc.CreateAttribute("version");
attr.Value = "2.0";
docNode.Attributes.Append(attr);
rssDoc.AppendChild(docNode);
// Create the channel element and its children
XmlElement chanNode = rssDoc.CreateElement("channel");
docNode.AppendChild(chanNode);
workNode = rssDoc.CreateElement("title");
workNode.InnerText = _title;
chanNode.AppendChild(workNode);
workNode = rssDoc.CreateElement("link");
workNode.InnerText = _link;
chanNode.AppendChild(workNode);
workNode = rssDoc.CreateElement("description");
workNode.InnerText = _description;
chanNode.AppendChild(workNode);
workNode = rssDoc.CreateElement("language");
workNode.InnerText = _language;
chanNode.AppendChild(workNode);
// Add item nodes to the channel
for (int index = 0; index < _items.Count; index++)
{
currentItem = (RssItem)_items[index];
itemNode = rssDoc.CreateElement("item");
chanNode.AppendChild(itemNode);
workNode = rssDoc.CreateElement("joke");
workNode.InnerText = currentItem.Joke;
itemNode.AppendChild(workNode);
workNode = rssDoc.CreateElement("date");
workNode.InnerText = currentItem.Date;
itemNode.AppendChild(workNode);
}
return (rssDoc.OuterXml);
}
private void Load()
{
_items = new ArrayList();
// Set channel properties from the profile:
_title = profile.SelectSingleNode("rss/channel/title").InnerText;
_link = profile.SelectSingleNode("rss/channel/link").InnerText;
_description = profile.SelectSingleNode("rss/channel/description").InnerText;
_language = profile.SelectSingleNode("rss/channel/language").InnerText;
string _source = profile.SelectSingleNode("rss/channel/source").InnerText;
// Retrieve items from the joke source
// input your code depending on the datasource
…
RssItem currentItem = new RssItem( /*joke text field*/,/*joke date field*/);
_items.Add(currentItem);
…
}
// internal class that implements the Joke object
internal class RssItem
{
private string _joke;
private string _date;
internal RssItem(string joke, string date)
{
_joke = joke;
_date = date;
}
public string Joke
{
get { return (_joke); }
set { _joke = value; }
}
public string Date
{
get { return (_date); }
set { _date = value; }
}
}
}
Recent Comments
Starting form the sample I mentioned
Thanks for posting this. It sav
The left image does not overlap t
Was Looking for the "inline" and "at