2013-02-13 31 views
2

我想从下面的URL中获取类似艺术家的名字,并将它们显示在列表框中。从XML文件读取单个节点到列表框(WP7,Silverlight,C#)

http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=ARTISTNAMEHERE & API_KEY = ff1629a695c346cc4b2dd1c41fcd4054

到目前为止,从其他人的问题,在这里我得到这个读取XML文件:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     WebClient wc = new WebClient(); 
     wc.DownloadStringCompleted += HttpCompleted; 
     wc.DownloadStringAsync(new Uri("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=ARTISTNAMEHERE&api_key=ff1629a695c346cc4b2dd1c41fcd4054")); 
    } 

    private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None); 

      // do something with the XDocument here 
     } 
    } 

正如我以前说过,我想带该页面中的艺术家姓名,即每个姓名节点并将其显示在列表框中。我会如何去做这件事?

回答

0

这应做到:

 XElement artists = xdoc.Root.Element("similarartists"); 
     if (artists != null) 
     { 
      IEnumerable<string> names = artists 
       .Elements("artist") 
       .Select(el => el.Element("name").Value); 
     } 

它使用它们用于使用LINQ to检索XML数据System.Xml.Linq命名空间的方法。

+0

我在控制台应用程序中测试了这个,但是这不能编译为Windows Phone? – 2013-02-13 08:32:27

+0

我环顾IEnumerable的其他例子,因为我从来没有使用它,发现它的工作原理,如果我IEnumberable 后添加一个变量名称。现在只有错误是它说System.Xml.Linq.Element不包含Select的定义。有任何想法吗?非常感谢迄今! – TheMarron 2013-02-13 08:34:35

+0

我添加了一些检查来处理如果无法找到艺术家名字的情况。 – 2013-02-13 08:36:10