|
-
August 26th, 2009, 04:23 AM
#1
Reading XML documents in C#
Hi Everyone,
Im currently developing a weather application that forecats weather etc...I have ran into a problem with the reading of the XML feed that im using.
Here is an example of an XML feed that is being used:
<?xml version="1.0" ?>
- <weatherdata>
- <weather weatherlocationcode="wc:UKXX0085" weatherlocationname="London, GBR" zipcode="" encodedlocationname="London%2c+GBR" url="http://weather.msn.com/local.aspx?wealocations=wc:UKXX0085&q=London%2c+GBR" imagerelativeurl="http://blst.msn.com/as/wea3/i/en-us/" degreetype="F" provider="Foreca" attribution="Data provided by Foreca" attribution2="© Foreca" lat="51.5178591" long="-0.1022164" timezone="1" alert="">
<current temperature="64" skycode="32" skytext="Clear" date="2009-08-26" day="Wednesday" shortday="Wed" observationtime="09:50:00" observationpoint="London / Heathrow Airport" feelslike="64" humidity="77" windspeed="14" winddisplay="14 mph S" />
<forecast low="61" high="70" skycodeday="11" skytextday="Showers" date="2009-08-26" day="Wednesday" shortday="Wed" precip="80" />
<forecast low="54" high="74" skycodeday="30" skytextday="Partly Cloudy" date="2009-08-27" day="Thursday" shortday="Thu" precip="40" />
<forecast low="50" high="65" skycodeday="39" skytextday="Showers / Clear" date="2009-08-28" day="Friday" shortday="Fri" precip="75" />
<forecast low="49" high="68" skycodeday="28" skytextday="PM Clouds" date="2009-08-29" day="Saturday" shortday="Sat" precip="5" />
<forecast low="59" high="67" skycodeday="30" skytextday="Cloudy / PM Sun" date="2009-08-30" day="Sunday" shortday="Sun" precip="10" />
<toolbar timewindow="60" minversion="1.0.1965.0" />
</weather>
</weatherdata>
Hopefully you can make it out but for each day of the forecast the same element name is used, in this case <forecast>. The problem im having is when i read in the data from the forecast element it seems to only bring back the first instance and last instance of the forecast elements. so effectively im able to get todays weather and the weather 4 days from now as thats the last entry. I use the following code in my application:
else if ((reader.NodeType == XmlNodeType.Element) && ((reader.Name == "forecast")
&& (firstForecastDone == false)))
{
firstForecastDone = true;
reader.MoveToAttribute("high");
int.TryParse(reader.Value, out MaxTemp);
currentWeatherReport.MaxTemperatureForecast = MaxTemp;
reader.MoveToAttribute("low");
int.TryParse(reader.Value, out MinTemp);
currentWeatherReport.MinTemperatureForecast = MinTemp;
reader.MoveToAttribute("day");
currentWeatherReport.Day1 = reader.Value;
I then repeat this code incrementing the values by 1 hoping that it would simply move to the next instance of the forecast element giving me the next days weather but it does not, as i mentioned it only brings back the current days weather and the weather 4 days from now.
Does anyone know how i might code this so that its possible to read each element seperately?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|