Yasserzaid’s Weblog

December 27, 2011

Google Weather with ASP.Net

Filed under: ASP.Net — yasserzaid @ 9:37 am

Hi all,

try this example to user Google Weather with ASP.Net

Now we will Create a Method to get Weather for Location


public static void GoogleWeather(string location)
 {
 HttpWebRequest GoogleRequest;
 HttpWebResponse GoogleResponse = null;
 XmlDocument GoogleXMLdoc = null;
 try
 {
 GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + string.Format(location));
 GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
 GoogleXMLdoc = new XmlDocument();
 GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
 XmlNode root = GoogleXMLdoc.DocumentElement;
 XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
 HttpContext.Current.Response.Write("<B>City : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</B>");
 XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");
 HttpContext.Current.Response.Write("<TABLE cellPadding=5><TBODY><TR>");
 HttpContext.Current.Response.Write("<TD><B><BIG><NOBR>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</NOBR></BIG></B>");
 HttpContext.Current.Response.Write("<B>Current:</B> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "");
 HttpContext.Current.Response.Write("" + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "");
 HttpContext.Current.Response.Write(nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText);
 nodeList = root.SelectNodes("descendant::weather/forecast_conditions");
 foreach (XmlNode nod in nodeList)
 {
 HttpContext.Current.Response.Write("</TD><TD align=middle>" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "");
 HttpContext.Current.Response.Write("<IMG alt='" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "' src='http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "'>");
 HttpContext.Current.Response.Write(nod.SelectSingleNode("low").Attributes["data"].InnerText + "°F | ");
 HttpContext.Current.Response.Write(nod.SelectSingleNode("high").Attributes["data"].InnerText + "°F");
 }
 HttpContext.Current.Response.Write("</TD></TR></TBODY></TABLE>");
 }
 catch (System.Exception ex)
 {
 HttpContext.Current.Response.Write(ex.Message);
 }
 finally
 {
 GoogleResponse.Close();
 }
 }

To use this method :-

GoogleWeather("Egypt");

Hope this helps

Good Luck

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.