Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:17+00:00 2026-06-13T22:45:17+00:00

This is likely to be a(nother) noob question, but I’m not sure how to

  • 0

This is likely to be a(nother) noob question, but I’m not sure how to do this.

I have piece of code within a private method which refers to a static method.

using (WebClient wc = new WebClient())
{
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    wc.DownloadStringAsync(new Uri(requestUri));
}

The static method it refers to:

static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{

    var xmlElm = XElement.Parse(e.Result);

    var status = (from elm in xmlElm.Descendants()
                  where elm.Name == "status"
                  select elm).FirstOrDefault();
    if (status.Value.ToLower() == "ok")
    {
        var res = (from elm in xmlElm.Descendants()
                   where elm.Name == "formatted_address"
                   select elm).FirstOrDefault();
        formatted = res.Value;

    }          
}

Now I need the content of the static method to replace the content of the WebClient.
Like:

using (WebClient wc = new WebClient())
{
    var xmlElm = XElement.Parse(e.Result);

    var status = (from elm in xmlElm.Descendants()
                  where elm.Name == "status"
                  select elm).FirstOrDefault();
    if (status.Value.ToLower() == "ok")
    {
        var res = (from elm in xmlElm.Descendants()
                   where elm.Name == "formatted_address"
                   select elm).FirstOrDefault();
        formatted = res.Value;

    }
}

Since I’m not sure where “e” originates from, I do not know how to get it working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:45:18+00:00Added an answer on June 13, 2026 at 10:45 pm

    You seem to be missing the point of asynchronous call. The whole point in this is to start the execution of the asynchronous method then keep going with your code forgetting all about the method, leaving only a callback method to handle its response.

    To make things short and simple, there is no way you can use the variable formatted the way you want, as you can use the result of the asynchronous call only in the callback method.

    You can however have the whole code inside the same block (no need for separate method) using such lambda syntax for anonymous method:

    myLabel.Text = "loading...<br />";
    using (WebClient wc = new WebClient())
    {
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler((sender2, e2) =>
        {
            var xmlElm = XElement.Parse(e2.Result);
            var status = (from elm in xmlElm.Descendants()
                where elm.Name == "status"
                select elm).FirstOrDefault();
            if (status.Value.ToLower() == "ok")
            {
                var res = (from elm in xmlElm.Descendants()
                    where elm.Name == "formatted_address"
                    select elm).FirstOrDefault();
                myLabel.Text = res.Value;
            }
            else
            {
                 myLabel.Text = "Bad status: " + status.Value;
            }
        });
        wc.DownloadStringAsync(new Uri(requestUri));
    }
    

    This for example will populate a label in the page with the results, once they are ready.

    If you want to wait until the load is finished, simply use the ordinary DownloadString method:

    string formatted = "";
    using (WebClient wc = new WebClient())
    {
        string raw = wc.DownloadString(new Uri(requestUri));
        var xmlElm = XElement.Parse(raw);
        var status = (from elm in xmlElm.Descendants()
            where elm.Name == "status"
            select elm).FirstOrDefault();
        if (status.Value.ToLower() == "ok")
        {
            var res = (from elm in xmlElm.Descendants()
                where elm.Name == "formatted_address"
                select elm).FirstOrDefault();
            formatted = res.Value;
        }
        else
        {
             formatted = "Bad status: " + status.Value;
        }
    }
    
    //use the variable as you wish...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

,This question is likely subjective, but a lot of grid Javascript plugins have come
This is likely a a novice question, but google surprisingly did not provide an
Unfortunately, this may not be a valid Code-Golf question as it is likely Javascript
This is likely a stupid question but I always find myself wondering which is
I think this is likely to be a generic .NET assembly loading question, but
Ok so this is likely a ridiculously stupid question but I can't seem to
This is most likely not an easy one but here is the situation: I
I know this is most-likely a simple question but when you restore a database
This is likely a very simple question with a straightforward answer but I'm something
This question might be specific to SoThink Video Encoder v2.5, but it might not.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.