Click here to Skip to main content
15,850,547 members
Articles / Programming Languages / XML

RSS Enclosure Downloader

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jun 2008CPOL3 min read 31.9K   169   12   4
Using LINQ to XML to download RSS enclosures.


Most people are familiar with RSS (Really Simple Syndication) as a method of downloading blog posts, but it can also be used as a way to download files. Using the <enclosure> tag within an item allows a file to be associated with the item, like an attachment on an email.

This mechanism is used predominantly by podcasters to distribute their shows.

There are many applications that allow podcasts to be downloaded, with all kinds of additional bells and whistles, but I wanted a simple application I could run as a schedule task (even if I wasn't logged in) to download my favourite podcasts.


This project uses some of the new XML features of VB.NET 9.0. XML Literals are used to create and update a download history XML file, and LINQ to XML is used to find any enclosures that have yet to be downloaded.

Using the Code

The project has been implemented as a Console application for simplicity sake, but could easily be converted into a Windows application, or even a Windows Service. All the work is done by a single sub-routine, DownloadRSSEnclosures, which takes the URL of the RSS feed and the folder to download the enclosures to as parameters. These parameters are passed to the sub-routine from command line arguments.

The first thing DownloadRSSEnclosures does is check for the existence of a download history file in the download folder for the feed. If it exists, it loads it into _DownloadHistoryXml; if not, it uses XML Literals to create a new XML document, ready to hold the download history.

_DownloadHistoryXml = <?xml version="1.0" encoding="UTF-8"?><history/>

Using XML Properties, another new feature in VB.NET, the history node is located in order to set the last-download-date attribute. Because an XML document could contain multiple history nodes, DownloadHistoryXml.<history> actually returns an IEnumerable of XElement. We know there will only be a single node, so we can use the LINQ Single() extension method to select that single node, before going on to set its last-download-date attribute to the current date/time.

_DownloadHistoryXml.<history>.Single().SetAttributeValue("last-download-date", DateTime.Now)

Downloading the RSS XML is trivial; simply calling the XDocument's static Load() method will return a new XDocument containing the XML. Slightly less trivial is the LINQ to XML that is used to select all the URLs to enclosures in the RSS feed that are not already in the download history.

Dim _NewEnclosures = _ 
        From enclosure In _RssXml.<rss>.<channel>.<item>.<enclosure> _ 
        Where Not (From download In _DownloadHistoryXml.<history>.<download> Select 
            download.@url).Contains(enclosure.@url) _ 
        Select enclosure.@url

With the LINQ returning a list of the URLs that have not been downloaded, a simple For Each loop can be used to download each file, save it to the download folder, and update the history (saving it after each file is downloaded in case the application is terminated before it completes). The only point of interest in the download loop is the way in which the download is added to the history XML file. Notice how XML Literals are used again to create the XML node (with an wmbedded expression to insert the URL), which is added to the history node.

_DownloadHistoryXml.<history>.Single().Add(<download url=<%= _EnclosureUrl %>/>)


  • 3 June 2008 - Original article published.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Assign IT Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHow to add PUBDATE criteria? Pin
bobotmedia31-Jul-09 8:18
bobotmedia31-Jul-09 8:18 
AnswerRe: How to add PUBDATE criteria? Pin
bobotmedia31-Jul-09 10:52
bobotmedia31-Jul-09 10:52 
GeneralFormatting broken Pin
leppie3-Jun-08 3:25
leppie3-Jun-08 3:25 
GeneralRe: Formatting broken Pin
Darren Fieldhouse4-Jun-08 1:03
Darren Fieldhouse4-Jun-08 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.