您的位置:首页 > 其它

【转】 使用Yahoo的公开API做天气预报

2008-08-25 20:20 399 查看
在Yahoo的Developer Network

http://developer.yahoo.com/weather/

详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥

首先了解Yahoo Weather Api的RSS Response格式:

Console Codes

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: