Parsing a PodCast in C#
October 2, 2007
Recently I assimilated the Windows Media Player into one of my projects, today I decided to add PodCast capabilities.
I was surprised how easy this was, I guess I’d expected more of a challenge. I went into this knowing nothing about PodCasts, once I realized it was RSS I knew this would be easy. RSS used to be Rich Site Summary, until people figured out how useful it was and renamed it Really Simple Syndication.
I know this is really simple code, frankly that’s part of why I’m posting it. Simple examples save people time. I tried to find a quick Copy+Paste solution for Podcasts on Google just before I wrote this. After three minutes of searching I quit and spent five writing this.
The example I wrote is a console application (yes, it runs from the DOS prompt) that grabs a Podcast page and loops through all the Podcast listings. I use console apps for examples because there isn’t a lot too them. This helps coders quickly Copy+Paste the code they need from my examples.
So without further adieu, this is how simple it is to do Podcasts in C#…
//This is always included in Console applications.
using System;
//This contains the XmlDocument, XmlNodeList and Node objects used below.
using System.Xml;
namespace ExamplePodCastParser
{
class Program
{
static void Main(string[] args)
{
//This loads the Podcast
XmlDocument doc = new XmlDocument();
doc.Load(“http://obama.senate.gov/podcast/index.xml”);
//This builds a list of the Item nodes
XmlNodeList items = doc.SelectNodes(“//item”);
//This loops through the list and writes out the title and URL.
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine(items[i].SelectSingleNode(“title”).InnerText);
Console.WriteLine(items[i].SelectSingleNode(“enclosure”).Attributes[“url”].Value);
}
}
}
}
Click here to download the Visual Studio project.
January 5, 2008 at 1:37 pm
Wow! That is so easy. I’d have never thought of this. Maybe because whenever I touch on System.Xml I get a hard on and go so much in to detail!
Thanks!
January 29, 2009 at 5:42 pm
Really great article.
October 3, 2012 at 7:46 am
Exactly what I was looking for – Thanks!