0

I am developing my first windows App and I am facing some Problems while parsing an Xml,the code is as shown below

 public void TimeParsing(string lat, string lon)
       {

           string urlbeg = "http://api.geonames.org/timezone?lat=";
           string urlmid = "&lng=";
           string urlend = "&username=dheeraj_kumar";


           WebClient downloader = new WebClient();
           Uri uri = new Uri(urlbeg + lat + urlmid + lon + urlend, UriKind.Absolute);
           downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TimeDownloaded);
           //downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TimeDownloaded);
           downloader.DownloadStringAsync(uri);
       }

     private void TimeDownloaded(object sender, DownloadStringCompletedEventArgs e)
     {
         if (e.Result == null || e.Error != null)
         {
             MessageBox.Show("Invalid");
         }
         else
         {
             XDocument document = XDocument.Parse(e.Result);
             var data1 = from query in document.Descendants("geoname")
                         select new Country
                         {
                             CurrentTime = (string)query.Element("time"),



                         };
             foreach (var d in data1)
             {
                 time = d.CurrentTime;
                 MessageBox.Show(d.CurrentTime);
                 // country = d.CountryName;

             }

         }

     }

The problem is that the Delegate TimeDownloaded is not being called. I used the same technique is parse a different URL and it was done easily but its not working in this case.Kindly Help me as I am pretty new to this field. Thanks in advance.

3
  • If you try to do it synchronously, does that work? .DownloadString instead of .DownloadStringAsync? Commented Jun 3, 2014 at 11:45
  • i was about to suggest same because on your server you can block multiple query to your website from same location if that server do that a secondary async would reset the query string sent and the first call will simply die on itself and second one could possibly die too. Commented Jun 3, 2014 at 11:48
  • @Jon_Lindeheim...no it isn't working,in fact it throws an error saying that there is no definition for .DownloadString. Commented Jun 3, 2014 at 11:50

2 Answers 2

1

Theres a few misses regarding fetching the nodes

The output is geonames/timezone/time, it's corrected below, also testable using the method DownloadStringTaskAsync instead

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public async Task TestMethod1()
    {
        await TimeParsing("-33.8674869", "151.20699020000006");

    }

    public async Task TimeParsing(string lat, string lon)
    {

        var urlbeg = "http://api.geonames.org/timezone?lat=";
        var urlmid = "&lng=";
        var urlend = "&username=dheeraj_kumar";
        var downloader = new WebClient();
        var uri = new Uri(urlbeg + lat + urlmid + lon + urlend, UriKind.Absolute);
        downloader.DownloadStringCompleted += TimeDownloaded;
        var test = await downloader.DownloadStringTaskAsync(uri);

        Console.WriteLine(test);
    }

    private void TimeDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            Console.WriteLine("Invalid");
        }
        else
        {
            var document = XDocument.Parse(e.Result);
            var data1 = from query in document.Descendants("timezone")
                        select new Country
                        {
                            CurrentTime = (string)query.Element("time"),



                        };

            foreach (var d in data1)
            {
                Console.WriteLine(d.CurrentTime);
            }

        }

    }
}

internal class Country
{
    public string CurrentTime { get; set; }
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

you can use the below mentioned code.

        Uri uri = new Uri(urlbeg + lat + urlmid + lon + urlend, UriKind.Absolute);
          HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(uri);
        //This time, our method is GET.
         WebReq.Method = "GET";
        //From here on, it's all the same as above.
        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

        //Now, we read the response (the string), and output it.
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        string s = _Answer.ReadToEnd();

        XDocument document = XDocument.Parse(s);
         var data1 = from query in document.Descendants("geoname")
                     select new Country
                     {
                         CurrentTime = (string)query.Element("time"),



                     };
         foreach (var d in data1)
         {
             time = d.CurrentTime;
             MessageBox.Show(d.CurrentTime);
             // country = d.CountryName;

         }

for Windows Phone 8 you have to implement the getResponse Method.

 public static System.Threading.Tasks.Task<System.Net.WebResponse> GetResponseAsync(this System.Net.WebRequest wr)
{
    return Task<System.Net.WebResponse>.Factory.FromAsync(wr.BeginGetResponse, wr.EndGetResponse, null);
}

1 Comment

actually I am using .GetResponse() method isn't present for Windows Phone 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.