Getting Syndication feeds (RSS/ATOM) in .NET
A while back I went through the trouble of creating a component that would load RSS feeds. While interesting it would take much less effort to do the same task with today's .NET framework. Check out this article for more information on syndication feeds. To learn how to create your own syndication feed check out this article.
Here are the namespaces that we need to include for the syndication feed to work
using System.ServiceModel.Syndication; using System.Xml;
Now we load the feed. The load method of the SyndicationFeed class takes a uri providing much desired flexibility as to the location of our feed
public static class SydincationFeedManager
{
public static SyndicationFeed Get(string uri)
{
return SyndicationFeed.Load(XmlReader.Create(uri));
}
}
To output the feed to the browser as RSS using ASP.NET MVC simply do the following
public class RssResult : ActionResult
{
public SyndicationFeed Feed { private get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/rss+xml";
Rss20FeedFormatter formatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
formatter.WriteTo(writer);
}
}
}
To output the feed to the browser as ATOM using ASP.NET MVC simply do the following
public class AtomResult : ActionResult
{
public SyndicationFeed Feed { private get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/atom+xml";
Atom10FeedFormatter formatter = new Atom10FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
formatter.WriteTo(writer);
}
}
}
For your ASP.NET web forms application you would do something like this in your Render events
Response.Clear();
Response.ContentType = "application/atom+xml";
//Rss20FeedFormatter formatter = new Rss20FeedFormatter(Feed);
Atom10FeedFormatter formatter = new Atom10FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(Response.Output))
{
formatter.WriteTo(writer);
}
Response.Flush();
Response.End();
To learn how to create your own syndication feed check out this article
Comments are always welcomed.
I am very much glad to know about the Social Media Syndication, This is really such a nice video. I have veeiwd you video and found it is an interesting one. Anyways keep it up and keep continue with your valuable thoughts.