Tagged by 'RSS Feed'

  • I always found writing code to read an RSS feed within my .NET application very time-consuming and long-winded. My RSS code was always a combination of using WebRequest, WebResponse, Stream, XmlDocument, XmlNodeList and XmlNode. That’s a lot of classes just to read an RSS feed.

    Yesterday, I stumbled on an interesting piece of code on my favourite programming site, where someone asked how to parse an RSS feed in ASP.NET. The answer was surprisingly simple. RSS feeds can now be consumed using the System.ServiceModel.Syndication namespace in .NET 3.5 SP1. All you need is two lines of code:

    var reader = XmlReader.Create("");
    var feed = SyndicationFeed.Load(reader);

    Here’s a full example on how we can iterate through through the SyndicationFeed class:

    public static List<BlogPost> Get(string rssFeedUrl)
        var reader = XmlReader.Create(rssFeedUrl);
        var feed = SyndicationFeed.Load(reader);
        List<BlogPost> postList = new List<BlogPost>();
        //Loop through all items in the SyndicationFeed
        foreach (var i in feed.Items)
            BlogPost bp = new BlogPost();
            bp.Title = i.Title.Text;
            bp.Body = i.Summary.Text;
            bp.Url = i.Links[0].Uri.OriginalString;
        return postList;

    That’s too simple, especially when compared to the 70 lines of code I normally use to do the exact same thing.

  • Ok! I admit it! I posted some incorrect information from one of my previous blog posts to “Dynamically Output A List of YouTube Video’s In ASP.NET”. I stupidly said: “The RSS feed is not structured in a nice enough format to output all the information you may need with ease.” I must have been drunk when I wrote that. As you can see from a sample of their RSS feed below I was wrong:

        <category scheme='' term='bike'/>
        <category scheme='' term='Podcast'/>
        <category scheme='' term='Pedrosa'/>
        <category scheme='' term='motorcycles'/>
        <category scheme='' term='Honda+RC212V'/>
        <category scheme='' term='speed'/>
        <category scheme='' term='Suzuki+GSV-R800'/>
        <category scheme='' term=''/>
        <category scheme='' term='Sports' label='Sport'/>
        <category scheme='' term='Rossi'/>
        <category scheme='' term='motorcycle+road+racing'/>
        <category scheme='' term='motograndprix'/>
        <category scheme='' term='Yamaha+YZR+M1'/>
        <category scheme='' term='Mugello'/>
        <category scheme='' term='Italy'/>
        <category scheme='' term='Stoner'/>
        <category scheme='' term='Ducati+Desmosedici+GP8'/>
        <category scheme='' term='MotoGP'/>
        <category scheme='' term='Lorenzo'/>
        <title type='text'>MotoGP action from Mugello 2009</title>
        <content type='text'>The best of the action from the Gran Premio D&amp;#180;Italia Alice, the fifth round of the 2009 motogp World Championship.</content>
        <link rel='alternate' type='text/html' href=''/>
        <link rel='' type='application/atom+xml' href=''/>
        <link rel='' type='application/atom+xml' href=''/>
        <link rel='self' type='application/atom+xml' href=''/>
          <gd:feedLink href='' countHint='24'/>
          <media:category label='Sport' scheme=''>Sports</media:category>
          <media:description type='plain'>The best of the action from the Gran Premio D&amp;#180;Italia Alice, the fifth round of the 2009 motogp World Championship.</media:description>
          <media:keywords>MotoGP, Italy, Mugello, Podcast, Stoner, Pedrosa, Rossi, Lorenzo, Yamaha+YZR+M1, Ducati+Desmosedici+GP8, Honda+RC212V, Suzuki+GSV-R800, motorcycle+road+racing, motograndprix, motorcycles, bike, speed</media:keywords>
          <media:player url=''/>
          <media:thumbnail url='' height='90' width='120' time='00:00:38.500'/>
          <media:thumbnail url='' height='90' width='120' time='00:00:19.250'/>
          <media:thumbnail url='' height='90' width='120' time='00:00:57.750'/>
          <media:thumbnail url='' height='240' width='320' time='00:00:38.500'/>
          <media:title type='plain'>MotoGP action from Mugello 2009</media:title>
          <yt:duration seconds='77'/>
        <gd:rating average='4.862069' max='5' min='1' numRaters='29' rel=''/>
        <yt:statistics favoriteCount='10' viewCount='1055'/>

    Each “entry” element within the RSS feed represents a YouTube video. You are able to extrapolate all the important information about each movie such as Average Score, View Count, Thumbnail Images, Video Description, etc. Really useful stuff!

    You may be thinking: Why should I use an RSS feed to retrieve the video information rather than using the YouTube API? Well, using a YouTube API is definitely the easier and most straight-forward method. But what you should be aware that the API only works from .NET 2.0 onwards. There isn’t a YouTube API for .NET 1.1. Unfortunately, I only found this out when I tried to implement the API into one of my .NET 1.1 client sites.

    The code I have written below, reads the YouTube RSS feed and stores the information in a DataTable.

    private void GetYouTubeData(string YouTubeUrl)
        //Create DataTable to store specific YouTube information
        DataTable dtYouTubeVideoData = new DataTable();
        DataRow drYouTubeVideoData;
        //Link to YouTube RSS feed
        XmlTextReader rssReader = new XmlTextReader(YouTubeUrl);
        XmlDocument xmlDoc = new XmlDocument();
        //Download the XML (via the XmlTextReader)
        //Select all nodes starting with "entry"
        XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("entry");
        //For each "entry" element found
        foreach (XmlNode node in xmlNodeList)
            drYouTubeVideoData = dtYouTubeVideoData.NewRow();
            //Create a new document, to search through the inner contents
            XmlDocument innerXmlDocument = new XmlDocument();
            // Get movie ID
            drYouTubeVideoData["YouTubeID"] = innerXmlDocument.GetElementsByTagName("id")[0].InnerText.Replace("", "");
            // Get movie title
            drYouTubeVideoData["Title"] = innerXmlDocument.GetElementsByTagName("title")[0].InnerText;
            //Get movie description
            drYouTubeVideoData["Description"] = innerXmlDocument.GetElementsByTagName("content")[0].InnerText;
            //Get the thumbnails
            XmlNodeList mediaTumbnail = innerXmlDocument.GetElementsByTagName("media:thumbnail");
            //Iterate through each thumbnail and only get one thumbnail per <entry>.
            foreach (XmlNode thumbnailNode in mediaTumbnail)
                if (thumbnailNode.Attributes["height"].Value == "90" && thumbnailNode.Attributes["url"].Value.EndsWith("1.jpg"))
                    drYouTubeVideoData["ImageUrl"] = thumbnailNode.Attributes["url"].Value;
            //Get movie rating
            XmlNodeList ratings = innerXmlDocument.GetElementsByTagName("gd:rating");
            foreach (XmlNode ratingsNode in ratings)
                drYouTubeVideoData["AverageRatings"] = ratingsNode.Attributes["average"].Value;
            //Get Statistics
            XmlNodeList statistics = innerXmlDocument.GetElementsByTagName("yt:statistics");
            foreach (XmlNode statisticsNode in statistics)
                drYouTubeVideoData["ViewCount"] = statisticsNode.Attributes["viewCount"].Value;
        //Bind YouTube data to repeater
        repVideoList.DataSource = dtYouTubeVideoData;