
Introduction
After looking around for a simple, customizable ASP.NET control that reads weather and forecast data from weather.com's xml feed...and finding nothing, I decided to grab my slideruler, put on my pocket protector, fire up Visual Studio and create one myself.
This ascx control displays current weather and weather forecast data on your ASP.NET website straight from an xml feed on weather.com. This may not be the prettiest or most efficient way of accomplishing this, but it's very easy to implement and extremely easy to customize. All you need to do is switch a few variables in the ascx file and you're off and running.
Oh...and this control also caches the xml data from weather.com so it doesn't ping the xml data with every user request. I have it set to clear and refresh the cached data every 15 minutes, but you can set it to any time frame you want...or even turn off caching if you so chose.
Without further adieu, let's get started.
Background
VERY IMPORTANT: You will first need to register on weather.com for their xml weather service. They provide this all for FREE. This will get you a License Key, PartnerID, and LocationID which are all required to use this control. So....go to weather.com now and get 'em.
https://registration.weather.com/ursa/xmloap/step1??
Got 'em? You're 90% there...Keep reading.
Using the code
OK...so you have your weather.com account information. Now, download the zip file at the top of this web page and unzip it. Next, do the following:
Step1: copy the WeatherControl.ascx file into your web project
Step2: copy and paste the entire ccicons folder into your web project
Step3: copy the css folder into your web project (contains stylesheet.css)
Step4: enter your own weather.com account data into the WeatherControl.ascx file (see below)
'********* account specific variables *********
Dim LicenseKey As String = "-------CHANGE ME-------"
Dim PartnerID As String = "-------CHANGE ME-------"
Dim LocationID As String = "-------CHANGE ME-------"
'**********************************************
NOTE: For LocationID visit: http://www.notkewl.com/myweather/
Step5: in any aspx file you want to use this weather control, add the following code (see the default.aspx file for example):
<%@ Register TagPrefix="My" TagName="Weather" Src="WeatherControl.ascx" %>
And add this where you want the generated weather table to show up on your web page:
<My:Weather ID="Weather2" runat="server" />
That's it!
You can go into the ascx file and, at the bottom, change the format of the table to whatever layout you want. Also, you can change a few more settings in there (right underneath the weather.com variables), and also change the font styles, background, etc. right in the css file.
Here are the settings you can change:
- Enable Weather Caching
- Set Time to Refresh the Cache
- Set Number of Days to Forecast
- Show the Date in the Forecast Table
- Show the Current Wind
- Set the Size of the Forecast Icons
- Show the "Last Updated: xxx" Line
- Display Temps in Celsius
- Display Wind Speed in KPH
If you want to customize it further...go nuts. You can pull even more data out of the weather.com xml file as well. Just use the two "Node Reading" functions at the top. See the code for examples on how to navigate through the xml tags...pretty self explanatory
Downlod The Source Code