wp7天气预报源代码(二序列化谷歌数据)公布源代码下载地址
由于代码过多,和繁杂的前台页面效果代码,没办法在博文中说明白,还有很多网友要求我公布源代码项目。在文章的最下面我提供了源代码的下载地址。
作者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来实现了一些效果。
摘自 韩弈风