-1

I'm trying to remove a node from an xml file, everything works, but sometimes it happens that the xml web page does not load. this is my code:

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

                    var lyric = doc.Descendants(XName.Get("Lyric", "http://api.chartlyrics.com/")).FirstOrDefault();
                    TextBlock1.Text = lyric.Value;


            }

        }
    }

    private void btnLoad_Click(object sender, RoutedEventArgs e)
    {



        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad));


    }

I' ve read to use the WebException to handle this "mistake" but I am not able to use it. can someone give me some help?

anderZubi
  • 6,414
  • 5
  • 37
  • 67
Riccardo Gai
  • 421
  • 1
  • 6
  • 19

1 Answers1

1

Have you tried this way?

    private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += HttpsCompleted;
            wc.DownloadStringAsync(new Uri("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad"));
        }
        catch (WebException ex)
        {
            // Check the exception here
        }
    }

And check for error in the handler too:

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

                var lyric = doc.Descendants(XName.Get("Lyric", "http://api.chartlyrics.com/")).FirstOrDefault();
                TextBlock1.Text = lyric.Value;
        }       
    }
    else
    {
        // Check for error here
    }
}

I realized it sometimes return Error, and I think the problem is in the server, because if I access it from the web browser, I sometimes get the result, but many times I get an error.

anderZubi
  • 6,414
  • 5
  • 37
  • 67
  • yes, if the url doesn't work and then nothing is displayed, it does't catch the exception! thanks for your patience :) – Riccardo Gai Jun 07 '13 at 08:56
  • I have completed the answer. In your code you don't check if the server return an error. I think the problem is on the server, because accessing the url from web browser happens the same. – anderZubi Jun 07 '13 at 09:03
  • I know it is a server error, if it is used too many times does not load the page, but I still can not use the exception, not even in the "else" can not handle the error: \ – Riccardo Gai Jun 07 '13 at 09:19
  • Are you sure? I put next line inside the else block, and I can see the error: `MessageBox.Show(e.Error.Message);` – anderZubi Jun 07 '13 at 09:23