I am making a program for Windows phone that will get weather data from a xml file and display it on screen. However, I keep getting null values and I am not sure what I am doing wrong.
Sample XML file: http://forecast.weather.gov/MapClick.php?lat=44.52160&lon=-87.98980&FcstType=dwml
Here is what I have:
try
{
// get the stream containing the response from the async call
streamResult = forecastState.AsyncResponse.GetResponseStream();
// load the XML
XElement xmlWeather = XElement.Load(streamResult);
// find the source element
XElement xmlCurrent = xmlWeather.Descendants("source").First();
// get city and height
xmlCurrent = xmlWeather.Descendants("location").First();
mTempCityName = (string)(xmlCurrent.Element("city"));
mTempHeight = (int)(xmlCurrent.Element("height"));
//Find the forecast time
xmlCurrent = xmlWeather.Descendants("time-layout").First();
//store time of day in array
mTimeOfDay = (string)(xmlCurrent.Attribute("period-name"));
//Find the temperature
xmlCurrent = xmlWeather.Descendants("temperature").First();
mTemp = (int)(xmlCurrent.Element("value"));
//now get the current weather conditions for each time period
xmlCurrent = xmlWeather.Descendants("weather").First();
mDescription = (string)(xmlCurrent.Attribute("weather-summary"));
//now get icon links for weather
xmlCurrent = xmlWeather.Descendants("conditions-icon").First();
mIcon = (string)(xmlCurrent.Attribute("icon-link"));
}
Well:
time-layoutdoesn’t have an attribute calledperiod-name(it has subelements with that attribute)weatherdoesn’t have aweather-summaryattribute – it has subelements with that attributeconditions-icondoesn’t have anicons-linkattribute, it hasicons-linkelementsIn other words, for each bit you need to look at exactly what the XML contains, and then exactly what you’re asking for, being careful to distinguish between elements and attributes.
Note that for the values you’re casting to
int(which don’t need extra brackets around them, btw) you should be getting an exception in this case – you clearly can’t be getting actual null values. Is it possible that an exception is being thrown and you’re not noticing it? What’s in your catch block?