c# - XML Document not loading from web URL - NullReferenceException -
this question has answer here:
- what nullreferenceexception, , how fix it? 32 answers
i want load weather information openweathermap.org's api xml document.
public weather() { /* generates custom weather url */ this.urlstub= @"http://api.openweathermap.org/data/2.5/forecast/daily?q="; this.location = "glasgow,uk"; this.apikey = "&appid=6911e84eacde075fdbdfaf05b9a2aaf5"; this.mode = "&mode=xml"; this.forecastweatherurl = urlstub + location + apikey + mode; } public bool loadxml() { /* loads xml info web */ try { this.forecastweatherxml.loadxml(forecastweatherurl); return true; } catch (system.nullreferenceexception ex) { console.out.writeline("error loading xml doc\n" + ex.stacktrace); return false; } } private void loadweatherbtn_click(object sender, eventargs e) { weather weather = new weather(); console.out.writeline(weather); // prints generated xml url if (weather.loadxml()) { console.out.writeline("xml loaded"); }
the loadweatherbtn_click method part of different class. output is:
> weather url: http://api.openweathermap.org/data/2.5/forecast/daily?q=glasgow,uk&appid=6911e84eacde075fdbdfaf05b9a2aaf5&mode=xml error loading xml doc @ al_fresgow.weather.loadxml() in c:\rootdirectory\appname\appname\weather.cs:line 163
the generated url shown in output works correctly why isn't loading?? program need wait load first (calculation time xml doc 0.0085 secs)?
i have assume type of this.forecastweatherxml
xmldocument
, though posted code not show declaration.
based on that, primary problem use of loadxml()
method expects xml string input. msdn
try using method load()
loads xml document specified url:
this.forecastweatherxml.load(forecastweatherurl);
Comments
Post a Comment