Resource Page DescriptionThis is a VB application that converts a users iTunes playlist to a Zune playlist. It demonstrates LINQ and VB 2008's unique XML literals to transform the iTunes Playlist XML file into a Zune playlist XML file.
The original code for this application was written by Avner Aharoni -- see:
http://blogs.msdn.com/xmlteam/archive/2006/12/18/converting-simple-itunes-playlists-to-zune-playlists-using-vb-9-0.aspx LINQ Code This application uses LINQ to query the iTunes playlist XML file. It then creates an IEnumerable of XML to be added to a new Zune playlist file.
//multi-line
' get the "Dict" element
Dim query = From key In iTuneLib...<key> _
Where key.Value = "Playlist ID" _
AndAlso key.ElementsBeforeSelf("string").Value = playListName _
Select key.Parent
Dim playListDict = query(0)
' put something here
Dim mediaList = From mediaID In playListDict.<array>...<integer>, _
key In iTuneLib.<dict>.<dict>.<key> _
Where mediaID.Value = key.Value _
Select key.ElementsAfterSelf("dict")(0)
' now return the properly formed xml for the Zune file
Return From key In mediaList.Elements("key") _
Where key.Value = "Location" _
Let location = key.ElementsAfterSelf("string").Value _
Let firstColon = location.IndexOf(":") _
Let index = location.IndexOf(":", firstColon + 1) _
Let newLocation = location.Substring(index - 1) _
Let file = New Uri(Uri.UnescapeDataString(newLocation)).LocalPath _
Select <media src=<%= file %> tid=<%= Guid.NewGuid() %>/>
XML Literal The XML literal is used to create the actual Zune playlist. It calles the LINQ code (above) to create a set of <seq> elements in the Zune playlist.
//multi-line
' define the Zune Playlist xml
Dim playlist = <?xml version="1.0"?>
<?zpl version="2.0"?>
<smil>
<head>
<meta name="generator" content="Zune -- 2.1.888.2"/>
<meta name="itemCount" content="2"/>
<meta name="totalDuration" content="553825"/>
<meta name="averageRating" content="0"/>
<title><%= txtZunePlaylistName.Text %></title>
</head>
<body>
<seq>
<%= GetMediaElements(cboiTunesPlaylist.SelectedItem) %>
</seq>
</body>
</smil>