使用 C# 和 Yahoo API 做天气预报
2008-10-14 15:16
381 查看
使用 C# 和 Yahoo API 做天气预报
转载至:http://www.pin5i.com/showtopic-19941.html在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式:
RSS Response
Code
[copy to clipboard]CODE:
<?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, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link>
<description>Yahoo! Weather for Tangshan, CH</description>
<language>en-us</language>
<lastBuildDate>Fri, 22 Aug 2008 8:00 am CST</lastBuildDate>
<ttl>60</ttl>
<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! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url></url>
</image>
<item>
<title>Conditions for Tangshan, CH at 8:00 am CST</title>
<geo:lat>39.63</geo:lat>
<geo:long>118.17</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link>
<pubDate>Fri, 22 Aug 2008 8:00 am CST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="67" date="Fri, 22 Aug 2008 8:00 am CST" />
<description>
<![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 67 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 86 Low: 69<br />
Sat - Partly Cloudy. High: 88 Low: 70<br />
<br />
<a
href="http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full
Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</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_CST</guid>
</item>
</channel>
</rss>
我们所需要用到的Node是//rss/channel/item/yweather:forecast
在这里我们用XmlDocument来实现,
Console Codes
Code
[copy to clipboard]CODE:
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
相关文章推荐
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 使用Yahoo的公开API做天气预报
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 【转】 使用Yahoo的公开API做天气预报
- 【转】 使用Yahoo的公开API做天气预报
- C#使用API屏蔽系统热键和任务管理器
- 如何在C#中使用Win32 API和其他库
- C#中使用API(SHFileOperation)进行文件操作,特别说明了回收站相关参数
- [C#]使用控制台获取天气预报
- Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能
- WebDriver基本API使用手册(基于Java和C#)
- 微信公众平台使用百度API查询天气预报
- C#使用API屏蔽系统热键和任务管理器
- C# winform使用系统API播放音乐(非调用控件)
- Android使用XmlPullParser解析XML以及天气预报信息api使用
- c# winform使用API屏蔽系统热键和任务管理器
- C#编程使用Managed Wifi API连接无线SSID
- C#编程使用Managed Wifi API连接无线SSID & C#编程使用Managed Wifi API连接无线SSID