【发布时间】:2014-10-14 20:01:20
【问题描述】:
所以我试图关注这个视频here 并实施 Yahoo!天气 API (XML) 到我在 Visual Basic 2013 pro 中的 webform 项目中。
但是当我在调试模式下运行项目时,我收到了 NullReferenceException 错误。
这是截图-
还有我遇到问题的代码块-
private void Getweather()
{
string query = String.Format("http://weather.yahooapis.com/forecastrss?w=44418");
XmlDocument wData = new XmlDocument();
wData.Load(query);
XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", manager);
Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value;
Condition = channel.SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
Humidity = channel.SelectSingleNode("yweather:condition", manager).Attributes["hunidity"].Value;
Windspeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
Town = channel.SelectSingleNode("yweather:city", manager).Attributes["city"].Value;
TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["text"].Value;
TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value;
TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["Low"].Value;
}
我正在尝试在按钮单击事件中调用此方法 -
private void button1_Click(object sender, EventArgs e)
{
Getweather();
textBox1.Text = Town;
textBox2.Text = Temperature;
textBox3.Text = Condition;
textBox4.Text = Humidity;
textBox5.Text = Windspeed;
textBox6.Text = TFCond;
textBox7.Text = TFHigh;
textBox8.Text = TFLow;
}
最后是API
对编码很陌生..帮助表示赞赏! :)
【问题讨论】:
-
在抛出异常的那一行设置断点,看看什么是空的。应该是
.SelectSingleNode("yweather:condition", manager) -
@PoweredByOrange 谢谢……是的!但我该如何解决这个问题?有什么想法吗?
-
我认为您错过了 getWeather() 中 Condition 变量的第二个 SelectSingleNode 部分。再次查看您链接的视频
-
@envyM6 试试
channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value你错过了Item节点。 -
任何建议如何修改此代码以手动输入位置?
标签: c# visual-studio webforms visual-studio-2013