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.
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 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
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.
- September 4th, 2007 - Submitted Article