openweathermap.orgのAPIから天気情報をXMLドキュメントとして読み込みたいとします。XMLドキュメントがWeb URLから読み込まれていない - NullReferenceException
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 from 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");
}
loadWeatherBtn_Clickメソッドは、別のクラスの一部です。出力は:
> Weather URL: http://api.openweathermap.org/data/2.5/forecast/daily?q=glasgow,uk&APPID=6911e84eacde075fdbdfaf05b9a2aaf5&mode=xml
Error loading xml Doc
at Al_Fresgow.Weather.loadXML() in C:\ROOTDIRECTORY\APPNAME\APPNAME\Weather.cs:line 163
出力に表示される生成されたURLは正しく動作しますが、なぜこのロードではありませんか?プログラムは最初に読み込むのを待つ必要があります(xml docの計算時間はわずか0.0085秒です)。
どのようなタイプ 'ForecastWeatherXml'のですか? –
'ForecastWeatherXml = new XmlDocument()'を初期化しましたか?それで、それは動作します。 – Neyoh
ご協力いただきありがとうございます。フィールド 'code'XmlDocument forecastWeatherXmlを設定します。 this.forecastWeatherXml = new XmlDocument();これを 'code'として初期化していませんでした。それは今働く、ありがとう再び – djmcdee