Click here to Skip to main content
15,891,689 members
Articles / Programming Languages / C#
Article

Yahoo! Weather Reader/Wrapper

Rate me:
Please Sign up or sign in to vote.
4.77/5 (7 votes)
11 Sep 20073 min read 64K   3K   45   7
A library meant to simplify getting the current weather conditions (as well as a 2 day forecast) into your .NET programs.
Screenshot - YahooWeatherDemo.png

Introduction

This is my first Code Project article and I hope everyone likes it. The library is meant to simplify getting the current weather conditions (as well as a 2 day forecast) into your .NET programs. I have created various objects to wrap the functionality provided by the Yahoo! Weather RSS Feed. I have enumerated various variables in an attempt to make the library simpler to use - I hope I haven't taken it too far. To get your local forecast, you need to find the Location ID from the Yahoo! Weather Website by searching for your city/town and getting the Location ID from the URL in the address bar.

Background

I was writing a program that reads the current weather conditions to the user using text to speech and I needed a simple way to access this information from my application. Once I started writing the code for the weather conditions, I figured I'd wrap it up in a Class Library for use in other projects.

Using the code

The code should be pretty straightforward to use and is well documented. I have included the XML Documentation generated by Visual Studio as well.

The bulk of the code consists of objects to hold the data returned by the RSS feed. The main class is the Weather class. It's the object you'll create when using the Library.

To create a new Weather object, simply use the following:

C#
Weather weatherObj = new Weather
        ("LOCATIONID",Weather.TemperatureUnits.Celcius);

Here, LOCATIONID is the Location Id that you get from the Yahoo! Weather Website (see above). As you can see, I have enumerated the Temperature Units to make the code simpler. I think it might be a bit of an overkill to have enumerated all of the Units like this, but I'm hoping to get around to adding support for other units of measurement as well as conversion between them.

The method in the Weather class that does all the work is the Refresh() method. Have a look at the source code, it's well-commented and should be easy to figure out what's going on in there. The basic approach is to get the RSS feed, parse out the relevant data using XmlTextReader and load the parsed data into the custom objects/properties - performing any conversions as necessary. The Refresh() method is called in the Weather constructor so you don't need to explicitly call it unless you wish to refresh the forecast data.

Have a look at Yahoo! Weather RSS Feed Documentation Page. It is where I got all my information from and it's the source of most of the commenting in the code.

Points of Interest

The TemperatureUnits used in the Weather constructor will decide the units of all the other returned data, so if you use TemperatureUnits.Celcius, the data returned will be in Metric units and if you use TemperatureUnits.Fahrenheit, the data returned will be in Standard units.

I would also like to point out that the feed returns the forecast for today and tomorrow. This data is stored in the Forecast object. The Forecast object contains a property/collection called Days which contains each forecasted day's data from the RSS feed. Have a look at the actual RSS feed source (View source in browser) to get an idea of how data is returned.

One last thing to note is that I haven't done any proper error-handling, that will be up to you to take care of.

History

  • September 4th, 2007 - Submitted Article

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
I like to write code. Is that 2000 chars yet?

Comments and Discussions

 
BugConvert 12-hour to 24-hour time Pin
yatharthz29-Oct-13 2:50
yatharthz29-Oct-13 2:50 
BugBugs Pin
temperory16-Sep-12 23:58
temperory16-Sep-12 23:58 
GeneralMy vote of 5 Pin
aofeng325-Jun-12 0:48
aofeng325-Jun-12 0:48 
GeneralMy vote of 4 Pin
aofeng325-Jun-12 0:44
aofeng325-Jun-12 0:44 
GeneralException related to wind speed Pin
The Cake of Deceit6-Aug-08 11:33
The Cake of Deceit6-Aug-08 11:33 
GeneralRe: Exception related to wind speed Pin
John Wcb1-Sep-08 15:17
John Wcb1-Sep-08 15:17 
I found the reason. The RSS data is as follows:

<channel>

<title>Yahoo! Weather - Burnaby, CA</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Burnaby__CA/*http://weather.yahoo.com/forecast/CAXX0051_c.html</link>
<description>Yahoo! Weather for Burnaby, CA</description>
<language>en-us</language>
<lastBuildDate>Mon, 01 Sep 2008 5:00 pm PDT</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Burnaby" region="" country="CA" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<yweather:wind chill="17" direction="120" speed="8.05" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<yweather:atmosphere humidity="63" visibility="1607.69" pressure="1024" rising="0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<yweather:astronomy sunrise="6:29 am" sunset="7:53 pm" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Burnaby, CA at 5:00 pm PDT</title>
<geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">49.25</geo:lat>
<geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">-122.95</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Burnaby__CA/*http://weather.yahoo.com/forecast/CAXX0051_c.html</link>
<pubDate>Mon, 01 Sep 2008 5:00 pm PDT</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="17" date="Mon, 01 Sep 2008 5:00 pm PDT" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<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, 17 C<BR />
<BR /><b>Forecast:</b><BR />
Mon - Mostly Clear. High: 19 Low: 11<br />
Tue - Few Showers. High: 18 Low: 11<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Burnaby__CA/*http://weather.yahoo.com/forecast/CAXX0051_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Mon" date="1 Sep 2008" low="11" high="19" text="Mostly Clear" code="33" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<yweather:forecast day="Tue" date="2 Sep 2008" low="11" high="18" text="Few Showers" code="11" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" />
<guid isPermaLink="false">CAXX0051_2008_09_01_17_00_PDT</guid>
</item>
</channel>


From the data you can see Wind.Speed and Atmosphere.Visibility is not integer.
So you have to change them in the Weather.cs to:
Wind.Speed = (int)Math.Round(decimal.Parse(reader.GetAttribute("speed")));
...
Atmosphere.Visibility = (int)Math.Round(decimal.Parse(reader.GetAttribute("visibility")));
...
Why? Because if you test Convert.ToInt32("8.88"), it will throw an exception like what you got above.

John

GeneralAlso see... Pin
Ravi Bhavnani11-Sep-07 3:11
professionalRavi Bhavnani11-Sep-07 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.