views:

30

answers:

1

Im trying to pull tracks from the Itunes playlist which are unchecked in Itunes using LINQ XML for the first time.

Tracks which are unchecked have a disabled attribute in the "ITunes Music Library.xml" file which is not present if the track in checked.

Tryed using this code and serveral variants of but I either get all the songs or none.

  var songs = from song in XDocument.Load(@"C:\Itunes Music Library.xml").Descendants("plist").Elements("dict").Elements("dict").Elements("dict")
                           select new XElement("song", from key in song.Descendants("key") where song.Attribute("Disabled").Value == "True"  select song);

Also tryed

where song.Attribute("Disabled") != null select song


 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
    <key>Major Version</key><integer>1</integer>
    <key>Minor Version</key><integer>1</integer>
    <key>Application Version</key><string>9.2</string>
    <key>Features</key><integer>5</integer>
    <key>Show Content Ratings</key><true/>
    <key>Music Folder</key><string>file://localhost/C:/Documents%20and%20Settings/paulc.vario/My%20Documents/My%20Music/iTunes/iTunes%20Music/</string>
    <key>Library Persistent ID</key><string>D35B6099ED58C39C</string>
    <key>Tracks</key>
    <dict>
        <key>2350</key>
        <dict>
            <key>Track ID</key><integer>2350</integer>
            <key>Name</key><string>Fake</string>
            <key>Artist</key><string>Alexander O'Neal</string>
            <key>Album Artist</key><string>Alexander O'Neal</string>
            <key>Album</key><string>Hearsay</string>
            <key>Genre</key><string>R&#38;B/Soul</string>
            <key>Kind</key><string>Protected AAC audio file</string>
            <key>Size</key><integer>4817555</integer>
            <key>Total Time</key><integer>277128</integer>
            <key>Disc Number</key><integer>1</integer>
            <key>Disc Count</key><integer>1</integer>
            <key>Track Number</key><integer>5</integer>
            <key>Track Count</key><integer>11</integer>
            <key>Year</key><integer>2003</integer>
            <key>Date Modified</key><date>2005-11-28T20:47:53Z</date>
            <key>Date Added</key><date>2005-07-11T21:58:42Z</date>
            <key>Bit Rate</key><integer>128</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Volume Adjustment</key><integer>102</integer>
            <key>Play Count</key><integer>7</integer>
            <key>Play Date</key><integer>3336718161</integer>
            <key>Play Date UTC</key><date>2009-09-25T09:09:21Z</date>
            <key>Release Date</key><date>2003-01-28T08:00:00Z</date>
            <key>Artwork Count</key><integer>1</integer>
            <key>Persistent ID</key><string>5341AD103345EDA5</string>
                       <key>Disabled</key><true/>
            <key>Track Type</key><string>File</string>
            <key>Protected</key><true/>
            <key>Purchased</key><true/>
            <key>Location</key><string>file://localhost/C:/Documents%20and%20Settings/paulc.vario/My%20Documents/My%20Music/iTunes/iTunes%20Music/Alexander%20O'Neal/Hearsay/05%20Fake.m4p</string>
            <key>File Folder Count</key><integer>4</integer>
            <key>Library Folder Count</key><integer>1</integer>
        </dict>
        <key>2352</key>
A: 

This is not going to be nice, mainly because the format of the data is not well suited for querying in XML. Basically the key and its value is only determined by the order of the elements in the document. Code like this should work:

XDocument doc = XDocument.Load(@"myfile.xml");

var songs = doc.Descendants("dict").Where(d =>
    {
        XElement disabledKey = d.Elements("key").Where(key => key.Value == "Disabled").FirstOrDefault();
        XElement nextElement = disabledKey == null ? null : disabledKey.NextNode as XElement;
        return nextElement != null && nextElement.Name == "true";
    }).Select(d => new XElement("song", d));

foreach (var song in songs)
{
    Console.WriteLine(song);
}
Vitek Karas MSFT
More complex than I thought it would be. I should have picked an easier xml task to try out LINQ. Thanks.
Canacourse