How to get weather detail using Web Services in ASP.NET?


In this article i have explained about how to get current weather details using ASP.NET and webservices. If you want to know whether detail of any city in the world, then you select your country name and the city name in the application finally click submit button your selected City detail Temperature and Humidity display in the page using web services. Check weather detail using Web Services in ASP.NET

Learn how to get weather detail using Web Services in ASP.NET?


Description:
Another one important question I saw in the forum section how to get weather detail of my city using web services. Here I explained how to get weather details of your city or any other city in the world using web services.
Here in this example project I keep the two drop down list one for country and another one for city. After you select country automatically that country cities are loaded in the second drop down list you can select city and click the submit button that all you will get your selected city weather details.


protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
Label2.Text = "";
if ((DropDownList1.SelectedIndex == 0) || (DropDownList2.SelectedIndex == 0))
{
return;
}
string xmlResult = null;
string url;
url = "http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=" + DropDownList2.SelectedItem + "&CountryName=" + DropDownList1.SelectedItem + "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader resStream = new StreamReader(response.GetResponseStream());
XmlDocument doc = new XmlDocument();
xmlResult = resStream.ReadToEnd();
doc.LoadXml(xmlResult);
try
{
XmlDocument xl = new XmlDocument();
xl.LoadXml(doc.GetElementsByTagName("string").Item(0).InnerText);
XmlNodeList xnList = xl.SelectNodes("CurrentWeather");
foreach (XmlNode xn in xnList)
{
Label1.Text = "Temperature : " + xn["Temperature"].InnerText;
Label2.Text = "Humidity : " + xn["RelativeHumidity"].InnerText;
}
}
catch
{
Label1.Text = "City Name Not match with our site!";
Label2.Text = "";
return;
}
}

Source Code Detail:
Here with i have attached entire source code. Download it and try to check your city weather detail using web services.
Front End : ASP.NET
Code Behind : C#

Note : If you add any country in the first drop down list please check the exact country name in the XML file (placed in the same project source file name as Country.XML) and add it to the drop down list.

Conclusion:
The Weather is not common for all cities it is different in all places, using this project we can check or test our city weather details. I hope this one is help you to find out weather report.


Attachments

  • Get Weather Detail using Web Services (42806-101131-WeatherDetail.rar)
  • Comments

    Author: J22 Sep 2011 Member Level: Bronze   Points : 1

    How to add additional fields for information from this website like Wind Pressure? I added labels but it is not displaying.

    foreach (XmlNode xn in xnList)
    {
    Label1.Text = "Location : " + xn["Location"].InnerText;
    Label2.Text = "Time : " + xn["Time"].InnerText;
    Label3.Text = "Wind : " + xn["Wind"].InnerText;
    Label4.Text = "Visibility : " + xn["Visibility"].InnerText;
    Label5.Text = "SkyCondition : " + xn["SkyCondition"].InnerText;
    Label6.Text = "Temperature : " + xn["Temperature"].InnerText;
    Label7.Text = "DewPoint : " + xn["DewPoint"].InnerText;
    Label8.Text = "Humidity : " + xn["RelativeHumidity"].InnerText;
    Label9.Text = "Pressure : " + xn["Pressure"].InnerText;
    Label10.Text = "PressureTendency : " + xn["PressureTendency"].InnerText;
    Label11.Text = "Success : " + xn["Success"].InnerText;

    }

    Author: J23 Sep 2011 Member Level: Bronze   Points : 1

    Disregard the last post. It is displaying but I don't understand why all fields do not display for all weather locations. For example, temperature does not display for Ivory Coast cities.

    Author: Ravindran24 Sep 2011 Member Level: Diamond   Points : 0

    Hi

    what ever cities name given by www.webservicex.net that only load in the dropdownlist.

    Guest Author: chaitanya03 Aug 2012

    I have to display weather information for today,tomorrow and day after tomorrow.
    Now i able to see present temperature only

    How to get weather information for tomorrow and day after tomorrow?

    Guest Author: Sudipta01 Oct 2012

    Hi... It is working fine.. Thanks for the post... Could any one tell me how to set default Country/city on DropDownList by help of login ip.

    Guest Author: Teckisttle20 Jun 2014

    Where can I download the source code again? There isn't any download link available. btw, this is a very helpful post. Keep up the good work.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: