在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式:

RSS Response
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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 - Tangshan, CHtitle>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.htmllink>
<description>Yahoo! Weather for Tangshan, CHdescription>
<language>en-uslanguage>
<lastBuildDate>Fri, 22 Aug 2008 8:00 am CSTlastBuildDate>
<ttl>60ttl>
<yweather:location city="Tangshan" region="" country="CH"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="67" direction="290" speed="4" />
<yweather:atmosphere humidity="95" visibility="4.97" pressure="" rising="0" />
<yweather:astronomy sunrise="5:26 am" sunset="6:54 pm"/>
<image>
<title>Yahoo! Weathertitle>
<width>142width>
<height>18height>
<link>http://weather.yahoo.comlink>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gifurl>
image>
<item>
<title>Conditions for Tangshan, CH at 8:00 am CSTtitle>
<geo:lat>39.63geo:lat>
<geo:long>118.17geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.htmllink>
<pubDate>Fri, 22 Aug 2008 8:00 am CSTpubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="67" date="Fri, 22 Aug 2008 8:00 am CST" />
<description>
![]()
Current Conditions:
Mostly Cloudy, 67 F
Forecast:
Fri - Partly Cloudy. High: 86 Low: 69
Sat - Partly Cloudy. High: 88 Low: 70
Full Forecast at Yahoo! Weather
(provided by The Weather Channel)
]]>
description>
<yweather:forecast day="Fri" date="22 Aug 2008" low="69" high="86" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="23 Aug 2008" low="70" high="88" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">CHXX0131_2008_08_22_8_00_CSTguid>
item>
channel>
rss>
我们所需要用到的Node是//rss/channel/item/yweather:forecast
在这里我们用XmlDocument来实现,

Console Codes
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Xml;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
document.Load("http://xml.weather.yahoo.com/forecastrss?p=CHXX0131");
XmlNodeList nodes = document.GetElementsByTagName("forecast",
@"http://xml.weather.yahoo.com/ns/rss/1.0");
foreach (XmlNode node in nodes)
{
Console.WriteLine("日期:{0},星期:{1},天气:{2},温度:{3}°C 至 {4}°C",
node.Attributes["date"].InnerText,
node.Attributes["day"].InnerText,
node.Attributes["text"].InnerText,
FToC(int.Parse(node.Attributes["low"].InnerText)),
FToC(int.Parse(node.Attributes["high"].InnerText)));
}
}
private static string FToC(int f)
{
return Math.Round((f - 32) / 1.8,1).ToString();
}
}
}
简单的实现了天气预报的功能了,这里调用的是河北唐山的天气,需要其它地区的天气可以这里查找代码
http://weather.yahoo.com/China/CHXX/regional.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14734354/viewspace-432977/
,如需转载,请注明出处,否则将追究法律责任。