Listen to NY Times Podcasts with Silverlight 2.0
I have spent a lot of time recently coming up to speed with Silverlight 2.0. It is the alpha version of Silverlight that is programmable via .NET languages, such as C# and VB.NET. It has been an interesting adventure so far, and I am excited to see how this platform matures. Coming from a strong WPF background, I feel that there are many things missing in Silverlight, but the potential for a great cross-platform, cross-browser UI technology is definitely there. If you would like to read my thoughts about how Silverlight 2.0 compares to WPF, feel free to read this post on my other blog.
As of this writing SL has limited support for calling Web services. You can only call a service that is hosted on the server to which you deployed the SL application, and the data returned by that service must be serialized as JSON (instead of SOAP). Despite those limitations, you can still perform remote procedure calls and create data-driven applications. Add in the fact that Silverlight has the Downloader class, which can download resources from the Web, and you have some very powerful tools at your disposal. This post shows how I put those tools to use, by creating a simple Silverlight 2.0 application that allows you to listen to the latest podcasts published by The New York Times. You can download the source code at the bottom of this post.
Here is a screenshot of the application in action:

This application has several parts, none of which is too complicated.
- Silverlight UI - A Page that contains the various elements/controls seen in the above image. The Page's code-behind calls into a Web service that returns information about all of the podcasts to display, updates TextBlocks as the state of the application changes, and starts playing a podcast when the user selects an item in the list. The UI code and XAML can be found in the SilverlightApp project.
- Custom ListBox Control - Since the current build of Silverlight 2.0 has very few built-in controls, I used my AgListBox control to display all of the podcasts. I could have used the ListBox control provided in the SDK samples, but that control does not support keyboard navigation through its items. I wanted to have keyboard navigation, so I used my custom control instead. The AgListBox control source code can be found in the AgControls project.
- Web Service - The SL client app asynchronously calls a Web service which returns the name and URL of every available podcast. The service includes screen-scraping code that uses regular expressions to parse out the podcast information from a Web page downloaded from the New York Times web site. I would like to give special thanks to Karl Shifflett for his great advice on how to screen-scrape the HTML doc. The NytPodcastService and screen-scraping code is in the SilverlightHost project.
The first thing I did was follow Bob Familiar's lucid walkthrough of how to set up a Visual Studio solution so that you can develop and debug a Silverlight 2.0 application that consumes a Web service. Once I got past that initial hurdle, it was smooth sailing.
The NytPodcastService Web service is small and simple. Here is the Web method:

Unfortunately at the time of this writing there is no RSS feed to get the latest podcasts from the Times. Instead I download their Podcasts page and scrape the data I need from it. That method is called from the Web method, as seen above. Here is the screen-scraping code:

Once that Web service exists, the rest of the development effort is all about figuring out how to display the data and allow the user to play the podcasts. The UI seen in the demo application is rather primitive and ugly, but enhancing it should be straightforward now that the core application exists. The XAML for the Silverlight page is below:

The MediaElement named ‘_podcastPlayer' is what we use to play the podcasts, which are MP3 files. You do not see that element on the screen; but since podcasts have no video associated with them...that is OK with me. 
We will not review every line of code in the Page's code-behind, but I think it is interesting to see how the Silverlight app communicates with the Web service. The code seen below explains how this communication takes place.

If you are familiar with using Web services, this code should look very familiar. Since Silverlight 2.0 currently has a reduced version of the .NET framework, you can leverage your existing knowledge and skills when moving into Silverlight development.
Download the demo here. You will need Visual Studio 2008 and the Silverlight 2.0 alpha installed on your machine to build and run this solution. When running the application, be sure to set the SilverlightHost project as the Startup Project.