-4

I want to download all the album artist from last.fm, and each cover was named album title. But when start the program, I get an error: "Invalid URI: The URI is empty."

program code:

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

        }
svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    So? Is the URI empty? If so, when is it empty? What have you tried to fix the code? – svick Sep 15 '12 at 17:56
  • What line? Haven't you debugged? What is the value of URI, and why might it be empty? Questions you should have asked *yourself* before asking here. You're likely to get much better answers if you do some debugging and write the results into your question. – Adam Sep 15 '12 at 17:57
  • 2
    Your code have bugs. It's up to you to find them (Visual Studio provides integrated debugger if you missed it) and then ask question on things you can't figure out/don't underestand. The other approch is simply pay someone to develop code for you. – Alexei Levenkov Sep 15 '12 at 17:58
  • `http://stackoverflow.com/questions/9870909/deserialize-xml-in-complex-object` read this may be it helps you – Anant Dabhi Sep 15 '12 at 18:25

1 Answers1

0

You initialize your array as

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

but never set the value numArr[0] which is used in foreach (var c in numArr) (and there is no album with rank=0).

Isn't this much more easier?

 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();
L.B
  • 114,136
  • 19
  • 178
  • 224