使用 C# 和 Yahoo API 做天气预报

使用 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


<?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/*[url]http://weather.yahoo.com/forecast/CHXX0131_f.html[/url]</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/*[url]http://weather.yahoo.com/forecast/CHXX0131_f.html[/url]</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/*[url]http://weather.yahoo.com/forecast/CHXX0131_f.html[/url]">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


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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值