The Zune and Accessing your Zune Social Data via XLINQ

December 10th, 2007     |     6 Comments

A short while ago, I decided to buy a new Zune MP3 player. I was quite happy with my first-generation Zune and the free software upgrade it received, but the idea of choosing custom pre-selected artwork on my device was too tempting to pass up. Long story short, I gave in and purchased the 8GB player from zuneoriginals.net (click on pictures for larger image):

z1

z2

z3

One of the features provided via the new Zune software is the ability to create an online account from where you can keep track of the music you have listened to, share your musical tastes with others, etc. For example, the following is the page created for me after I signed up: http://social.zune.net/member/kirupa 

Besides seeing all of the awesome music I listen to, my social page has an interactive widget that allows anyone to browse through my recently played or favorite music:

zunecard

Anyway, what interested me more about all of this was having easy access to my musical data via a web service: http://social.zune.net/zcard/usercardservice.ashx?zunetag=kirupa (where you can replace kirupa with your account name to see your info). This opens up a lot of interesting things you can do, for the data returned is basically just XML.

That is where LINQ comes in. In previous posts, I described using LINQ to access data stored in a collection. With XML, you do not have data stored sequentially in an array-like structure. Your data is hierarchically organized with nodes and sub-nodes (and attributes!) representing the various information collected about your music playing habits. Our regular LINQ queries will only help to a certain point, but we need something more specialized. Fortunately, to make accessing nested XML data more direct, you have XLINQ, short for Language INtegrated Query for XML.

Describing all of XLINQ in this post will not do it justice, so I will cover this in greater detail as an article on kirupa.com at a future date. Instead, for now, let me first show you how to display all of the songs you’ve either marked as a favorite or played via the Zune media player recently:

XElement musicData = XElement.Load(@"http://social.zune.net/zcard/usercardservice.ashx?zunetag=kirupa");
 
var music = from m in musicData.Descendants()
            where m.Name.LocalName == "track"
            select m;
 
foreach (XElement m in music)
{
string song = m.Element("label").Value;

XElement album = m.Element("album");
 
string albumName = album.Element("label").Value;
string artist = album.Element("artist").Element("label").Value;
 
Console.WriteLine(song + " by " + artist + " from " + albumName);
Console.WriteLine("——————");
}

The first part where you see my music variable should look familiar to other LINQ statements you’ve seen before. The difference is that I am digging through a collection of XElement types where each XElement represents an element in your XML document. In this case, I am getting a list of all elements whose name happens to be "track".

The track element, or node in this case, is still pretty generic. You have several ways of drilling into your track node. You could continue checking its descendants and querying further, but for diversity, let’s access the nodes directly using the various methods XElement allows you to use:

string song = m.Element("label").Value;

XElement album = m.Element("album");
 
string albumName = album.Element("label").Value;
string artist = album.Element("artist").Element("label").Value;
 
Console.WriteLine(song + " by " + artist + " from " + albumName);
Console.WriteLine("——————");

Our track node contains a label node representing the song, and we can access that directly by passing in the node name label! The rest of our data, though, is nested inside an album node. To make our code more readable, I decided to create a new XElement storing just our album data.

With your album data stored, you can access the various nested information by traversing down your tree just like you did earlier when accessing your your song title. Album name is straightforward to get, but the artist information isn’t. The artist information is nested inside the label node of your artist node. I could create a new XElement for just our artist, but instead, I am just going to directly access the nested data:

string artist = album.Element("artist").Element("label").Value;

Notice that I am digging deeper by simply tagging more .Element(name) arguments to traverse down the right branch.

When you run the above code, the following information is displayed:

cmdOutput 

If you took a look at the original XML data, you’ll see that the few lines we wrote to filter through the hundreds of data points was well worth it! Anyway, I hope this article helped give you a brief overview of XLINQ and the Zune Social data.

Cheers!
Kirupa :)

6 Responses to “The Zune and Accessing your Zune Social Data via XLINQ”

  1. Blake Shannon Says:

    Microsoft wouldn’t give you a free new Zune? Shame.

  2. kirupa Says:

    They did give me a pony though!

  3. Krilnon Says:

    Does your pony behave at all like this pony?: http://www.youtube.com/watch?v=u-prMb6BdNs

    Also, I liked this section of your post: “choosing custom pre-selected”

    When do you listen to music on your Zune?

  4. kirupa Says:

    I’m glad somebody noticed that. Well it’s better than “you can have any color you want…as long as it’s Black!” :P

    The Zune social site updates with music played both on the device (when I sync) as well as via the media software. I listen to my Zune at work and when I’m working at home, but I have a Zen Vision for listening to music at night.

    :)

  5. kirupaBlog - If it isn’t broken, take it apart and fix it! » Blog Archive » WPF: Data Binding to XML Data using XPath Says:

    [...] For example, using the Zune Social XML file (see my earlier coverage of the Zune web service), I was able to create a simple WPF application that shows my most recent played songs in a Listbox control: [...]

  6. Zune Social on Silverlight (v1) - Zune Boards Says:

    [...] XLINQ stuff comes from a Silverlight/Expression dev who created a simple C# app to download it all: kirupaBlog – If it isn’t broken, take it apart and fix it! » Blog Archive » The Zune and Acces…. I’m sorta new to this stuff, so everything is pretty poorly hacked together. If you can offer any [...]

Leave a Reply