Weather Information from Google Weather using ASP.NET and LINQ to XML

Today, we will take advantage of Google Weather API to retrieve weather information for a given zip code and show it on our ASP.NET page. Google weather (http://www.google.com/ig/api?weather=96715) API return data in XML format and we will parse this data on our page using LINQ to XML and traverse through nodes to retrieve the returned information. This is a very basic example but once we know how to fetch one node information rest all is a piece of cake.

Snapshot:

ASP.NET Code:

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

<style type="text/css">

 

body

{

font-family:Arial;

}

#Panel1

{

 

width:400px;

 

}

#Panel1 img

{

float:left;

width:100px;

height:100px;

margin-top:10px;

}

#Panel1 p

{

 

float:right;

margin-top:-10px;

margin-right:0px;

 

}

#Panel1 legend

{

 

background-color:#3399ff;

color:White;

}

</style>

 

</head>

<body>

<form id="form1" runat="server">

<div>

Enter your zip code:<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>

<asp:Button ID="btnGo" runat="server" Text="GO" onclick="btnGo_Click" />

 

<asp:Panel ID="Panel1" runat="server">

<asp:Image ImageUrl="" runat="server" ID="icon" /> <br />

<p>

Current Condition: <b><asp:Label ID="currCondition" runat="server" Text=""></asp:Label></b><br />

Temprature in Fahrenheit: <b><asp:Label ID="temp_f" runat="server" Text=""></asp:Label></b><br />

Temprature in Celsius: <b><asp:Label ID="temp_c" runat="server" Text=""></asp:Label></b><br />

Humidity: <b><asp:Label ID="humidity" runat="server" Text=""></asp:Label></b><br />

Wind Condition: <b><asp:Label ID="wind_condition" runat="server" Text=""></asp:Label></b><br />

</p>

</asp:Panel>

</div>

</form>

</body>

</html>

 

 

Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;

using System.IO;

using System.Xml.Linq;

using System.Text;

 

public partial class GoogleWeather : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

 

 

}

 

public void getWeatherData()

{

// load xml result from Google weather

XDocument xd = XDocument.Load("http://www.google.com/ig/api?weather=" + txtZip.Text);

 

// navigate to current conditions node

var current_conditions = from currentCond in xd.Root.Descendants("current_conditions")

select currentCond;

 

// navigate to Forecast info node

var forcastInfo = from forecastinfo in xd.Root.Descendants("forecast_information")

select forecastinfo;

 

foreach (var item in forcastInfo)

{

StringBuilder sb = new StringBuilder();

sb.Append("");

sb.Append("Forecast for ");

// retriving information from nodes child attribute

sb.Append(item.Element("city").Attribute("data").Value);

sb.Append("<i>(Zip Code: "+item.Element("postal_code").Attribute("data").Value+")</i>");

sb.Append("<i>(Forecast Date: " + item.Element("forecast_date").Attribute("data").Value + ")</i>");

sb.Append("<i>(Current Date: " + (Convert.ToDateTime(item.Element("current_date_time").Attribute("data").Value)).ToUniversalTime().AddHours(-8).ToString() + ")</i>");

sb.Append("<i>(Unit System:" + item.Element("unit_system").Attribute("data").Value + ")</i>");

Panel1.GroupingText = sb.ToString();

 

}

 

foreach (var item in current_conditions)

{

currCondition.Text = item.Element("condition").Attribute("data").Value;

temp_f.Text = item.Element("temp_f").Attribute("data").Value;

temp_c.Text = item.Element("temp_c").Attribute("data").Value;

humidity.Text = item.Element("humidity").Attribute("data").Value;

icon.ImageUrl = "http://www.google.com" + item.Element("icon").Attribute("data").Value;

wind_condition.Text = item.Element("wind_condition").Attribute("data").Value;

 

}

 

 

 

}

protected void btnGo_Click(object sender, EventArgs e)

{

getWeatherData();

}

}

Tags: , , , ,

ASP.NET | LINQ | XML