iTunes Playlists to Xml: Parsing categories with LINQ

I recently had the need to determine what types of music I had within my collection of music. Since I created iTunes Playlists to Xml so that I could export out my library on my iPod, it was easy enough to run that and then open LINQPad to run the following query.

Obviously, you must export the Genre field in order for the following query to work.

// Location to the XML from iTunes Playlists to XML
String playlistXml = @“C:\Users\James\Projects\services\WcfRestService\App_Data\playlistXml.xml”;

XDocument xml = XDocument.Load(playlistXml);

var categories = from track in xml.Descendants(“track”) group track by track.Element(“genre”).Value into t orderby t.Key //orderby t.Count() descending select new { Category = t.Key, Count = t.Count() };

categories.Dump();

Note that you can switch the orderby lines depending upon how you want to sort the returned data.

This may be something I eventually build into my application, along with other reporting features. I know that I’d love to have some comparision functionality, either built-in or as another application, so that I can see how my music tastes change, or if I’m consistent.

Sample output

With some cleanup, sample output is as follows.

  • Alternative - 279
  • Alternative & Punk - 154
  • Alternative Rock - 282
  • Blues - 120
  • Books & Spoken - 145
  • Children’s Music - 12
  • Classical - 255
  • Country - 104
  • Dance - 76
  • Dance & DJ - 220
  • Easy Listening - 20
  • Electronic - 278
  • Electronica/Dance - 91
  • Folk - 319
  • French Pop - 90
  • Gospel & Religious - 18
  • Hard Rock & Metal - 6
  • Hip Hop/Rap - 88
  • Holiday - 18
  • House - 4
  • Industrial - 107
  • Industrial Metal - 17
  • Jazz - 30
  • J-Pop - 48
  • J-Rock - 23
  • Lo-Fi - 35
  • Metal - 82
  • New Age - 270
  • Pop - 1225
  • Pop/Rock - 26
  • R&B - 105
  • R&B/Soul - 81
  • Reggae - 30
  • Rock - 3125
  • Soundtrack - 348
  • World - 57