出处:http://dotnet.cnblogs.com/page/42993/?page=1
这个功能 主要参考以上文章,同时本人又添加了中文天气显示 和天气图片显示。
雅虎天气的API 详细介绍
http://developer.yahoo.com/weather/
首先了解Yahoo Weather Api的RSS Response格式
例如 成都
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Hua-yang, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Hua-yang__CH/*http://weather.yahoo.com/forecast/CHXX0052_f.html</link>
<description>Yahoo! Weather for Hua-yang, CH</description>
<language>en-us</language>
<lastBuildDate>Mon, 24 Oct 2011 1:59 pm CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Hua-yang" region="" country="CH" />
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph" />
<yweather:wind chill="75" direction="120" speed="4" />
<yweather:atmosphere humidity="50" visibility="4.35" pressure="30.09" rising="2" />
<yweather:astronomy sunrise="7:12 am" sunset="6:24 pm" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Hua-yang, CH at 1:59 pm CST</title>
<geo:lat>30.5</geo:lat>
<geo:long>104.07</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Hua-yang__CH/*http://weather.yahoo.com/forecast/CHXX0052_f.html</link>
<pubDate>Mon, 24 Oct 2011 1:59 pm CST</pubDate>
<yweather:condition text="Partly Cloudy" code="30" temp="75" date="Mon, 24 Oct 2011 1:59 pm CST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
<b>Current Conditions:</b><br />
Partly Cloudy, 75 F<BR />
<BR /><b>Forecast:</b><BR />
Mon - Partly Cloudy. High: 71 Low: 60<br />
Tue - Showers. High: 65 Low: 58<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Hua-yang__CH/*http://weather.yahoo.com/forecast/CHXX0052_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Mon" date="24 Oct 2011" low="60" high="71" text="Partly Cloudy" code="30" />
<yweather:forecast day="Tue" date="25 Oct 2011" low="58" high="65" text="Showers" code="11" />
<guid isPermaLink="false">CHXX0052_2011_10_25_7_00_CST</guid>
</item>
</channel>
</rss>
上面红色的两行,是查询当天和第二天的天气情况,我们要获取的天气信息就在里面
一、首先在vs中建立 web服务。添加代码如下:
public class Weather : System.Web.Services.WebService
{
[WebMethod(Description = "根据城市的名称返回该城市的天气预报情况,只能查询省会和直辖市的天气情况。")]
public DataSet GetWeather(string strCityName)
{
string strXml = "http://xml.weather.yahoo.com/forecastrss?p=CHXX" + CityNameToCityNum(strCityName);
XmlDocument Weather = new XmlDocument();
Weather.Load(strXml);
XmlNamespaceManager namespacemanager = new
XmlNamespaceManager(Weather.NameTable);
namespacemanager.AddNamespace("yweather",
"http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNodeList nodes = Weather.SelectNodes("/rss/channel/item/yweather:forecast", namespacemanager);
//XmlNodeList nodes = Weather.GetElementsByTagName("forecast", "http://xml.weather.yahoo.com/ns/rss/1.0");
DataSet dstWeather = new DataSet();
DataTable dtblWeather = new DataTable("Weather");
dstWeather.Tables.Add(dtblWeather);
dstWeather.Tables["Weather"].Columns.Add("Date", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("Week", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("Weather", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("WeatherCh", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("Tlow", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("Thigh", typeof(string));
dstWeather.Tables["Weather"].Columns.Add("Code", typeof(string));
if (nodes.Count > 0)
{
foreach (XmlNode node in nodes)
{
DataRow drowWeather = dstWeather.Tables["Weather"].NewRow();
drowWeather["Date"] = EmonthToCmonth(node.SelectSingleNode
("@date").Value.ToString());
drowWeather["Week"] = EweekToCweek(node.SelectSingleNode
("@day").Value.ToString()) + "(" + node.SelectSingleNode("@day").Value.ToString()
+ ")";
drowWeather["Weather"] = node.SelectSingleNode("@text").Value;
drowWeather["WeatherCh"] = EN_weather_ch(Convert.ToInt32(node.SelectSingleNode("@code").Value));
drowWeather["Tlow"] = FToC(int.Parse(node.SelectSingleNode
("@low").Value)) + "℃";
drowWeather["Thigh"] = FToC(int.Parse(node.SelectSingleNode
("@high").Value)) + "℃";
drowWeather["Code"] = node.SelectSingleNode("@code").Value;
dstWeather.Tables["Weather"].Rows.Add(drowWeather);
}
return dstWeather;
}
else
{
DataRow drowNone = dstWeather.Tables["Weather"].NewRow();
drowNone["Week"] = "None";
drowNone["Weather"] = "None";
drowNone["Tlow"] = "None";
drowNone["Thigh"] = "None";
dstWeather.Tables["Weather"].Rows.Add(drowNone);
return dstWeather;
}
return dstWeather;
}
/// <summary>
/// 从华氏转换成摄氏
/// </summary>
/// <param name="f">华氏度</param>
/// <returns></returns>
private string FToC(int f)
{
return Math.Round((f - 32) / 1.8, 1).ToString();
}
/// <summary>
/// 从星期英文缩写转汉字
/// </summary>
/// <param name="strEweek">星期的英文缩写</param>
/// <returns></returns>
private string EweekToCweek(string strEweek)
{
switch (strEweek)
{
case "Mon":
return "星期一";
break;
case "Tue":
return "星期二";
break;
case "Wed":
return "星期三";
break;
case "Thu":
return "星期四";
break;
case "Fri":
return "星期五";
break;
case "Sat":
return "星期六";
break;
case "Sun":
return "星期日";
break;
default:
return "传参错误";
break;
}
}
/// <summary>
/// 从月英文缩写转汉字
/// </summary>
/// <param name="strReplace">需要替换的年月日</param>
/// <returns></returns>
private string EmonthToCmonth(string strReplace)
{
return Convert.ToDateTime(strReplace).ToString("yyyy年MM月dd日");
}
/// <summary>
/// 根据城市名称返回城市编号
/// </summary>
/// <param name="strCityName">城市名称</param>
/// <returns></returns>
private string CityNameToCityNum(string strCityNameToNum)
{
//中国各个省会和直辖市对应的查询代码
Hashtable htWeather = new Hashtable();
htWeather.Add("北京", "0008");
htWeather.Add("天津", "0133");
htWeather.Add("杭州", "0044");
htWeather.Add("合肥", "0448");
htWeather.Add("上海", "0116");
htWeather.Add("福州", "0031");
htWeather.Add("重庆", "0017");
htWeather.Add("南昌", "0097");
htWeather.Add("香港", "0049");
htWeather.Add("济南", "0064");
htWeather.Add("澳门", "0512");
htWeather.Add("郑州", "0165");
htWeather.Add("呼和浩特", "0249");
htWeather.Add("乌鲁木齐", "0135");
htWeather.Add("长沙", "0013");
htWeather.Add("银川", "0259");
htWeather.Add("广州", "0037");
htWeather.Add("拉萨", "0080");
htWeather.Add("海口", "0502");
htWeather.Add("南宁", "0100");
htWeather.Add("成都", "0016");
htWeather.Add("石家庄", "0122");
htWeather.Add("贵阳", "0039");
htWeather.Add("太原", "0129");
htWeather.Add("昆明", "0076");
htWeather.Add("沈阳", "0119");
htWeather.Add("西安", "0141");
htWeather.Add("长春", "0010");
htWeather.Add("兰州", "0079");
htWeather.Add("西宁", "0236");
htWeather.Add("南京", "0099");
object cityNum = htWeather[strCityNameToNum];
if (cityNum == null)
{
return "City not found";
}
else
{
return cityNum.ToString();
}
}
/// <summary>
/// 雅虎天气 英文转中文
/// </summary>
/// <param name="code">天气代码</param>
/// <returns></returns>
private string EN_weather_ch(int code)
{
switch (code)
{
case 0: return "龙卷风 ";
case 1: return "热带风暴";
case 2: return "飓风";
case 3: return "严重的雷暴 ";
case 4: return "雷暴 ";
case 5: return "混合雨雪";
case 6: return "混合降雨和冰雹 ";
case 7: return "混合雪和雨夹雪 ";
case 8: return "冻结小雨 ";
case 9: return "小雨 ";
case 10: return "冻雨 ";
case 11:return "阵雨";
case 12: return "阵雨";
case 13:return "雪飘雪";
case 14: return "小雪阵雨";
case 15: return "吹雪";
case 16:return "雪";
case 17:return "冰雹";
case 18:return "雨夹雪";
case 19:return "尘埃";
case 20: return "雾";
case 21:return "霾";
case 22:return "黑烟";
case 23:return "大风";
case 24:return "风";
case 25:return "低温";
case 26:return "多云";
case 27:return "多云(晚上)";
case 28:return "多云(白天)";
case 29:return "局部多云(晚上)";
case 30:return "局部多云(天)";
case 31:return "清爽(晚)";
case 32:return "晴天";
case 33:return "晴朗(晚)";
case 34:return "晴朗(天)";
case 35:return "混合雨和冰雹";
case 36:return "热";
case 37:return "局部地区性雷暴";
case 38:return "零星雷暴";
case 39:return "零星雷暴";
case 40:return "零星阵雨";
case 41:return "大雪";
case 42:return "零星阵雪";
case 43:return "大雪";
case 44:return "多云";
case 45:return "雷阵雨";
case 46:return "阵雪";
case 47:return "局部雷阵雨";
default: return "3200无法使用";
}
}
}
二、调用方法,获取数据
我自己写的代码:
public string today_weather = "";//今天天气
public string today_Hight = "";//今天最高温度
public string today_Low = "";//今天最低温度
public string today_weather_img = "";//天气图片
//明天天气
public string tomorrow_weather = "";//今天天气
public string tomorrow_Hight = "";//今天最高温度
public string tomorrow_Low = "";//今天最低温度
public string tomorrow_weather_img = "";//天气图片
protected void Page_Load(object sender, EventArgs e)
{
Weather _w = new Weather();
DataTable _s=_w.GetWeather("成都").Tables[0];
if (_s.Rows.Count > 0)
{
today_weather = _s.Rows[0]["WeatherCh"].ToString();
today_Hight = _s.Rows[0]["Thigh"].ToString();
today_Low = _s.Rows[0]["Tlow"].ToString();
today_weather_img = "http://l.yimg.com/a/i/us/nws/weather/gr/" + _s.Rows[0]["Code"].ToString() + "n.png";//大图
//小图 "http://l.yimg.com/a/i/us/we/52/" + _s.Rows[1]["Code"].ToString() + ".gif";
tomorrow_weather = _s.Rows[1]["WeatherCh"].ToString();
tomorrow_Hight = _s.Rows[1]["Thigh"].ToString();
tomorrow_Low = _s.Rows[1]["Tlow"].ToString();
tomorrow_weather_img = "http://l.yimg.com/a/i/us/nws/weather/gr/" + _s.Rows[1]["Code"].ToString() + "n.png";//大图
}
}
(1)GetWeather方法是公共方法,提供Web调用。
(2)FToC方法,他主要是把RSS返回的华氏温度转换成摄氏温度,其实这一步可以不用的,当初没发现,URL中加点参数就返回的是摄氏温度。
(3)EweekToCweek方法,他主要是把英文的星期缩写变成中文。
(4)EmonthToCmonth方法,它主要是把英文的月份缩写变成中文,并重新排序。
(5)CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就只能支持这么几个城市了,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号。
(6)EN_weather_ch方法,主要是根据天气代码 获取中文的天气信息。雅虎返回的天气信息是英文的,此处是我在网上找的雅虎天气代码与的中文对应。如有错误请网友指正。共同进步。