使用LINQ XML处理ITunes播放列表

时间:2010-08-03 08:49:52

标签: linq-to-xml itunes

我正试图从Itunes播放列表中提取曲目,这些曲目是首次使用LINQ XML在Itunes中取消选中。

未选中的曲目在“ITunes Music Library.xml”文件中具有已禁用的属性,如果已选中该曲目,则该曲目不存在。

尝试使用此代码和几种变体,但我得到所有的歌曲或没有。

  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);

也试过

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">
<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>

1 个答案:

答案 0 :(得分:0)

这不会很好,主要是因为数据格式不适合用XML查询。基本上,密钥及其值仅由文档中元素的顺序决定。 像这样的代码应该有效:

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);
}