Bookmark and Share

SyndicationFeed and Beating FeedProxy

by KodefuGuru 8. September 2009 16:05

Reading in a series of blog posts is made easy utilizing classes that came in the .NET Framework 3.5. As long as the feed is in the Atom 1.0 or RSS 2.0 formats, you easily build your own aggregator service.

SyndicationFeed feed = null;
using (XmlReader reader = XmlReader.Create(feedUrl))
{
    feed = SyndicationFeed.Load(reader);
}
foreach (SyndicationItem item in feed.Items)
{
}

Off of the item, you will probably want to grab the Title.Text and the Summary.Text. Be sure to HtmlDecode them! Another important thing is the Categories collection, which I’m using as tags. Here’s the line of code from my project: Tags = item.Categories.Select(c => c.Name).Aggregate((a, b) => a + "," + b).

Lastly, an aggregator would be pointless unless you were grabbing the url (get it from Links[0].Uri.ToString()). However, many feeds are using Feedburner. If you pull from feedburner, the url will be a feedproxy.google.com url instead of one that goes directly to the blog article. The trick to get around this turned out to be quite simple. Make a WebRequest to the feedproxy url then read the WebResponse’s url.

private static string GetTrueUrl(string url)
{
    if (url.StartsWith("http://feedproxy.google.com"))
    {
        var request = WebRequest.Create(url);
        using (var response = request.GetResponse())
        {
            url = response.ResponseUri.ToString();
            response.Close();
        }
    }
    return url;
}

That’s all there is to reading in standard feeds. I’ll leave it up to you to decide on a specific implementation.

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
C# MVP, INETA Community Champion


MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er

I have joined Anti-IF Campaign


World Map

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010