【问题标题】:WebClient download many files error — Invalid URI: The URI is emptyWebClient 下载许多文件错误 — Invalid URI: The URI is empty
【发布时间】:2012-09-08 12:23:41
【问题描述】:

我想从 last.fm 下载所有专辑艺术家,每个封面都命名为专辑标题。 但是当启动程序时,我得到一个错误:“无效的 URI:URI 是空的。”

程序代码:

public static void GetXML()
        {
            string url = @"http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=radiohead&api_key=b25b959554ed76058ac220b7b2e0a026";
            string albumName = String.Empty;
            string coverLink = String.Empty;
            int[] numArr = new int[30];

            for (int x = 1; x < numArr.Length; x++)
            {
                numArr[x] = x;   
            }

           XDocument xml = XDocument.Load(url);

           foreach (var c in numArr)
           {
               var name = xml.XPathSelectElements(String.Format("//album[@rank='{0}']", c))
                             .Select(x => x.Element("name").Value)
                             .ToList();
               foreach (var item in name)
               {
                   albumName = item.ToString();
               }

               var covers = xml.XPathSelectElements(String.Format("//album[@rank='{0}']/image[@size='extralarge']", c))
                          .Select(x => x.Value)
                          .ToList();
               foreach (var item in covers)
               {
                   coverLink = item.ToString();
               }

               WebClient web = new WebClient();
               web.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(web_DownloadFileCompleted);
               web.DownloadFileAsync(new Uri(coverLink), @"X:\Code\T\" + albumName + ".jpg");
           }

        }

【问题讨论】:

  • 所以? URI 是否为空?如果有,什么时候是空的?您尝试过什么来修复代码?
  • 哪一行?你没调试过吗? URI 的值是什么,为什么它可能是空的?在问这里之前你应该问你自己的问题。如果您进行一些调试并将结果写入您的问题,您可能会得到更好的答案。
  • 您的代码有错误。您可以自行找到它们(如果您错过了 Visual Studio 会提供集成调试器),然后就您无法弄清楚/不理解的事情提出问题。另一种方法是花钱请人为你开发代码。
  • http://stackoverflow.com/questions/9870909/deserialize-xml-in-complex-object 阅读这可能对你有帮助

标签: c# webclient last.fm webclient-download


【解决方案1】:

您将数组初始化为

for (int x = 1; x < numArr.Length; x++)
{
      numArr[x] = x;   
}

但永远不要设置numArr[0] 中使用的值foreach (var c in numArr)(并且没有rank=0 的专辑)。

这不是更容易吗?

 var albums = xml.Descendants("album")
        .Select(a => new
        {
            Rank = (int)a.Attribute("rank"),
            Name = a.Element("name").Value,
            ImageUrl = a.XPathSelectElement("image[@size='extralarge']").Value
        })
        .ToList();

【讨论】:

    猜你喜欢
    • 2023-01-30
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2017-12-18
    相关资源
    最近更新 更多