I am writing a Windows Phone 7 app to pull weather data from WeatherBug’s API. I’m running into problems with the 7 day forecast data. I am getting the data in XML but I can’t parse the XML data.
Here is the code that calls the web service and passes the zip code:
public void GetForecastByZip(string zip)
{
string url = ("http://" + apiCode + ".api.wxbug.net/getForecastRSS.aspx?ACode=" + apiCode + "&zipCode=" + zip);
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
xmlClient.DownloadStringAsync(new Uri(url));
}
And here is the code where I am getting the XML and attempting to do… something with it.
void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
XmlReader reader = XmlReader.Create(xmlData);
XDocument doc = XDocument.Load(reader);
}
}
The format of the XML is like so:
<rss version="2.0" xmlns:georss="http://www.georss.org/georss">
<channel>
<title>Forecast for Williston Park, NY - USA</title>
<link>http://weather.weatherbug.com/NY/Williston Park-weather/local-forecast/7-day-forecast.html?ZCode=Z5546&Units=0</link>
<description>Weatherbug, the owner of the world's largest weather network is now providing an API to it's weather data in the
form of RSS. This will enable it's enthusiastic users to build their own applications.</description>
<language>en-us</language>
<lastBuildDate>Wed, 07 Dec 2011 20:56:00 GMT</lastBuildDate>
<ttl>60</ttl>
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0" />
<aws:WebURL>http://weather.weatherbug.com/NY/Williston Park-weather/local-forecast/7-day-forecast.html?ZCode=Z5546&Units=0</aws:WebURL>
<aws:forecasts type="Detailed" date="12/7/2011 3:56:00 PM">
<aws:location>
<aws:city>Williston Park</aws:city>
<aws:state>NY</aws:state>
<aws:zip>11596</aws:zip>
<aws:zone>NY179</aws:zone>
</aws:location>
<aws:forecast>
<aws:title alttitle="WED">Wednesday</aws:title>
<aws:short-prediction>Rain</aws:short-prediction>
<aws:image isNight="1" icon="cond014.gif">http://deskwx.weatherbug.com/images/Forecast/icons/cond014.gif</aws:image>
<aws:description>Wednesday</aws:description>
<aws:prediction>Rain. Highs in the upper 50s. North winds 5 to 10 mph with gusts up to 20 mph. Chance of rain near 100 percent.</aws:prediction>
<aws:high units="&deg;F">59</aws:high>
<aws:low units="&def;F">31</aws:low>
</aws:forecast>
<aws:forecast>
<aws:title alttitle="THU">Thursday</aws:title>
<aws:short-prediction>Sunny</aws:short-prediction>
<aws:image isNight="0" icon="cond007.gif">http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif</aws:image>
<aws:description>Thursday</aws:description>
<aws:prediction>Sunny. Highs in the lower 40s. West winds 15 to 20 mph... Diminishing to 5 to 10 mph in the afternoon.</aws:prediction>
<aws:high units="&deg;F">43</aws:high>
<aws:low units="&deg;F">35</aws:low>
</aws:forecast>
etc., etc.
The aws:forecast section occurs seven times – once for each day in the forecast. Each iteration of it has the title, short-prediction, image, description, prediction, high, and low elements.
I have a Windows Phone pivot page entitled Forecast.xaml with seven items. I’m aiming for a display like so for each day:
“City: Williston Park”
“Date: 12/7/2011”
“Day: Wednesday”
“Description: Rain”
“Prediction: Rain. Highs in the upper 50s. North winds 5 to 10 mph with gusts up to 20 mph. Chance of rain near 100 percent.”
“High: 59”
“Low: 31”
Nothing fancy. But I just can’t figure out a way to pull the individual elements from the file and assign them to a variable.
I’ve read the tutorial at Kotan Code, but I’m having trouble applying it to my project. Any help would be much appreciated.
You can use Linq to XML – only make sure that you declare and use the right namespace, in this case
awsas defined in your XML: