在上一篇文章里已經介紹了這個應用
文章源地址:http://www.cnblogs.com/wildfeng/archive/2012/03/21/2410504.html
由於代碼過多,和繁雜的前台頁面效果代碼,沒辦法在博文中說明白,還有很多網友要求我公布源代碼項目。在文章的最下面我提供了源代碼的下載地址。
作者QQ:29992379
這個天氣預報用的是谷歌的API,我特意寫了個工具類用來解析谷歌天氣數據,本文中主要介紹這個工具類。

代碼如下:
using System;
using System.Linq;
using System.Xml.Linq;
namespace GoogleWeather
{
public static class GoogleWeatherHelper
{
/// <summary>
/// 獲取城市以及省
/// </summary>
/// <param name="xmlWeather">xml數據</param>
/// <returns></returns>
public static string GetCity(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
return forecast_information.Element("city").Attribute("data").Value;
}
/// <summary>
/// 獲取中文城市名稱
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetPostalCode(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
return forecast_information.Element("postal_code").Attribute("data").Value;
}
/// <summary>
/// 獲取預報的日期
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetForecastDate(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_information").First();
return forecast_information.Element("forecast_date").Attribute("data").Value;
}
/// <summary>
/// 獲取濕度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHumidity(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("current_conditions").First();
return forecast_information.Element("humidity").Attribute("data").Value;
}
/// <summary>
/// 獲取風向
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetWindCondition(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("current_conditions").First();
return forecast_information.Element("wind_condition").Attribute("data").Value;
}
/// <summary>
/// 獲取今天星期
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTodayWeek(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
return forecast_information.Element("day_of_week").Attribute("data").Value;
}
/// <summary>
/// 獲取今天最低溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTodayLow(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
return forecast_information.Element("low").Attribute("data").Value;
}
/// <summary>
/// 獲取今天最高溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTodayHight(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
return forecast_information.Element("high").Attribute("data").Value;
}
/// <summary>
/// 獲取今天天氣圖標
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTodayIcon(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
}
/// <summary>
/// 獲取今天天氣情況
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTodayCondition(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").First();
return forecast_information.Element("condition").Attribute("data").Value;
}
/// <summary>
/// 獲取明天星期
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTomorrowWeek(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
return forecast_information.Element("day_of_week").Attribute("data").Value;
}
/// <summary>
/// 獲取明天最低溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTomorrowLow(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
return forecast_information.Element("low").Attribute("data").Value;
}
/// <summary>
/// 獲取明天最高溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTomorrowHight(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
return forecast_information.Element("high").Attribute("data").Value;
}
/// <summary>
/// 獲取明天天氣圖標
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTomorrowIcon(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
}
/// <summary>
/// 獲取明天天氣情況
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetTomorrowCondition(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(1);
return forecast_information.Element("condition").Attribute("data").Value;
}
/// <summary>
/// 獲取后天星期
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHouTianWeek(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
return forecast_information.Element("day_of_week").Attribute("data").Value;
}
/// <summary>
/// 獲取后天最低溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHouTianLow(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
return forecast_information.Element("low").Attribute("data").Value;
}
/// <summary>
/// 獲取后天最高溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHouTianHight(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
return forecast_information.Element("high").Attribute("data").Value;
}
/// <summary>
/// 獲取后天天氣圖標
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHouTianIcon(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
}
/// <summary>
/// 獲取后天天氣情況
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetHouTianCondition(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(2);
return forecast_information.Element("condition").Attribute("data").Value;
}
/// <summary>
/// 獲取大后天星期
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetDaHouTianWeek(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
return forecast_information.Element("day_of_week").Attribute("data").Value;
}
/// <summary>
/// 獲取大后天最低溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetDaHouTianLow(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
return forecast_information.Element("low").Attribute("data").Value;
}
/// <summary>
/// 獲取大后天最高溫度
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetDaHouTianHight(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
return forecast_information.Element("high").Attribute("data").Value;
}
/// <summary>
/// 獲取大后天天氣圖標
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetDaHouTianIcon(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
return ExtractFileName(forecast_information.Element("icon").Attribute("data").Value);
}
/// <summary>
/// 獲取大后天天氣情況
/// </summary>
/// <param name="xmlWeather"></param>
/// <returns></returns>
public static string GetDaHouTianCondition(XElement xmlWeather)
{
XElement forecast_information = xmlWeather.Descendants("forecast_conditions").ElementAt(3);
return forecast_information.Element("condition").Attribute("data").Value;
}
private static string ExtractFileName(string fullFileName)
{
string str = fullFileName.Substring(fullFileName.LastIndexOf('/') + 1);
return str.Substring(0, str.LastIndexOf('.')).Replace("cn_", "");
}
}
}
整個天氣預報項目源代碼的下載地址:http://download.csdn.net/detail/wildfeng04/4168595
在以后的博文里我會詳細講解這個應用UI方面的實現,畢竟這個應用亮點全在UI上面。我個人是這么理解的,因為功能代碼不是很難,UI的效果比較炫。我用了Storyboard來實現了一些效果。









