I developed a webapplication,Now i want to create Rss feeds for my website. In my application i have a module call Film news, which contain the latest news of the film stars. Now i want to create rss feeds for that module. the news contains title and description. How can i create rss feeds for my application?
Here's the code i use for my feed, It's just an .ashx (Generic handler), which I point my website at.
Remember to add this little piece of html to your site:
<link rel="alternate" type="application/rss+xml" href="/rss.ashx" title="Rss feed for yourdomain.com" />
And here's the complete handler code:
public class rssHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var Response = context.Response;
// Prepare response
Response.Buffer = false;
Response.Clear();
Response.ContentType = "application/rss+xml";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
// Create an XmlWriter to write the feed into it
using (XmlWriter writer = XmlWriter.Create(Response.OutputStream))
{
// Set the feed properties
SyndicationFeed feed = new SyndicationFeed
("yourdomain.com",
"Some description of your feed",
new Uri("http://www.yourdomain.com"));
// Add authors
feed.Authors.Add(new SyndicationPerson
("[email protected]",
"your name",
"http://www.yourdomain.com"));
// Add categories
NewsType[] categories = (NewsType[])Enum.GetValues(typeof(NewsType)); // NewsType is a enum I use, which is custom created
foreach (var category in categories)
{
feed.Categories.Add(new SyndicationCategory(category.GetDescription()));
}
// Set copyright
feed.Copyright = new TextSyndicationContent
("© Copyright 2009 your name");
// Set generator
feed.Generator = "yourdomain.com";
// Set language
feed.Language = "da-DK";
// Add post items
List<SyndicationItem> items = new List<SyndicationItem>();
var newsList = News.GetLatest(20);
foreach (var news in newsList)
{
string url = GetShowUrl(news);
SyndicationItem item = new SyndicationItem();
item.Id = news.ID.ToString();
item.Title = TextSyndicationContent.CreatePlaintextContent(news.Name);
item.Content = SyndicationContent.CreateXhtmlContent(news.Content);
item.PublishDate = news.DateCreated.Value;
item.Categories.Add(new SyndicationCategory(news.NewsType.Value.GetDescription()));
item.Links.Add(new SyndicationLink(new Uri(url), "alternate", news.Name, "text/html", 1000));
items.Add(item);
}
feed.Items = items;
// Write the feed to output
Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);
formatter.WriteTo(writer);
writer.Flush();
}
Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
private string GetShowUrl(News news)
{
// Returns proper absolute URL to the item
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With