Webform Application中的Yahoo Weather API实现

时间:2014-10-14 20:01:20

标签: c# visual-studio webforms visual-studio-2013

所以我试图关注这个视频here并实施Yahoo!在Visual Basic 2013专业版中将天气API(XML)导入我的webform项目。

但是我在调​​试模式下运行项目时出现NullReferenceException错误。

这是截图 -

enter image description here enter image description here enter image description here

代码块我遇到了问题 -

 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

编码很新...帮助赞赏! :)

1 个答案:

答案 0 :(得分:1)

这一行

       Condition = channel.SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;

必须

       Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;