Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML文件中的单个节点读入列表框(WP7,Silverlight,C)_C#_Xml_Silverlight_Windows Phone 7_Linq To Xml - Fatal编程技术网

C# 将XML文件中的单个节点读入列表框(WP7,Silverlight,C)

C# 将XML文件中的单个节点读入列表框(WP7,Silverlight,C),c#,xml,silverlight,windows-phone-7,linq-to-xml,C#,Xml,Silverlight,Windows Phone 7,Linq To Xml,我想从下面的URL中获取类似艺术家的姓名,并将其显示在列表框中 艺人姓名和api_key=ff1629a695c346cc4b2dd1c41fcd4054 与其他人的问题不同,我有以下内容来阅读XML文件: private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { WebClient wc = new WebClient(); wc.DownloadString

我想从下面的URL中获取类似艺术家的姓名,并将其显示在列表框中

艺人姓名和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
        }
    }
正如我之前所说的,我想从该页面获取艺术家的名字,这是每个名字节点,并将它们显示在一个列表框中。我该怎么做呢?

这应该可以做到:

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

它使用System.Xml.Linq名称空间中的方法,这些方法用于通过使用Linq检索Xml数据。

我在控制台应用程序中测试了这一点,但这不适用于Windows Phone?我查看了IEnumerable的其他示例,因为我以前从未使用过它,发现如果在IEnumerable之后添加一个变量名,它会起作用。现在唯一的错误是它说System.Xml.Linq.Element不包含Select的定义。有什么想法吗?非常感谢!我添加了一些检查来处理找不到艺术家姓名的情况;在你的.cs文件上。仔细检查,我肯定有。奇怪的