Click here to Skip to main content
Click here to Skip to main content
Go to top

Yahoo! Weather Reader/Wrapper

, 11 Sep 2007
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

El Chubb

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

Comments and Discussions

 
BugConvert 12-hour to 24-hour time Pinmemberyatharthz29-Oct-13 2:50 
BugBugs Pinmembertemperory16-Sep-12 23:58 
GeneralMy vote of 5 Pinmemberaofeng325-Jun-12 0:48 
GeneralMy vote of 4 Pinmemberaofeng325-Jun-12 0:44 
GeneralAlso see... PinmemberRavi Bhavnani11-Sep-07 3:11 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 11 Sep 2007
Article Copyright 2007 by El Chubb
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid