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.
Hi
what ever cities name given by www.webservicex.net that only load in the dropdownlist.