日期:2014-05-20  浏览次数:20849 次

求解。。。linq to xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
- <results>
- <songList>
- <!-- freemusic/song/result/Sb230d4b83cf4d4a9
  --> 
- <song>
  <id>Sb230d4b83cf4d4a9</id> 
  <name>You Belong With Me</name> 
  <artist>泰勒 史薇芙特(Taylor Swift)</artist> 
  <artistId>Ac097678888daa741</artistId> 
  <album>Fearless - International Edition</album> 
  <duration>232.0</duration> 
  <canBeDownloaded>true</canBeDownloaded> 
  <hasFullLyrics>true</hasFullLyrics> 
  <canBeStreamed>true</canBeStreamed> 
  <albumId>B1367683ceabfe43b</albumId> 
  <hasSimilarSongs>false</hasSimilarSongs> 
  <hasRecommendation>false</hasRecommendation> 
  </song>
- <!-- freemusic/song/result/S1dcbc954d664eb88
  --> 
- <song>
  <id>S1dcbc954d664eb88</id> 
  <name>Love Story</name> 
  <artist>泰勒 史薇芙特(Taylor Swift)</artist> 
  <artistId>Ac097678888daa741</artistId> 
  <album>Now That's What I Call Music 30</album> 
  <duration>233.0</duration> 
  <canBeDownloaded>true</canBeDownloaded> 
  <hasFullLyrics>true</hasFullLyrics> 
  <canBeStreamed>true</canBeStreamed> 
  <albumId>B5a5f1e0b80e94813</albumId> 
  <hasSimilarSongs>true</hasSimilarSongs> 
  <hasRecommendation>false</hasRecommendation> 
  </song>
  </songList>
  <estimatedResultCount>55</estimatedResultCount> 
  </results>

上面这个xml,我用var songs = from song in XElement.Load(@"http://www.google.cn/music/search?q=taylor%20swift&oq=taylor&aq=0&num=2&output=xml").Elements("song")
  select song;

这个语句为什么读不了呢?
求大牛指教,然后又换了
XDocument songList = XDocument.Load(@"http://www.google.cn/music/search?q=taylor%20swift&oq=taylor&aq=0&num=20&output=xml");
   
  var songs = from song in songList.Descendants("song")
  select song;
这样还是读取不了,最后获取的结果都是0.请问是哪里错了呢?

------解决方案--------------------
C# code

XDocument songList = XDocument.Load(@"http://www.google.cn/music/search?q=taylor%20swift&oq=taylor&aq=0&num=20&output=xml");

var songs = from song in songList.Descendants("song")
          select new {
                                  id= xmlLog.Element("id").Value,
                                  name= xmlLog.Element("name").Value,
                                  artist= xmlLog.Element("artist").Value
          };

------解决方案--------------------
C# code


XElement  songList = XElement .Load(@"http://www.google.cn/music/search?q=taylor%20swift&oq=taylor&aq=0&num=20&output=xml");
   
  var songs = from song in songList.Descendants("song")
  select song;

------解决方案--------------------
C# code

void Main()
{
    var xml=XElement.Load(@"http://www.google.cn/music/search?q=taylor%20swift&oq=taylor&aq=0&num=2&output=xml");
    var query=from x in xml.Descendants("song")
               select new {
                                  id= x.Element("id").Value,
                                  name= x.Element("name").Value,
                                  artist= x.Element("artist").Value
                                  //剩下的自己加吧  这样测试是可以读取到数据的
                              };
    Console.WriteLine(query);
}