【问题标题】:Yahoo Weather API implementation in Webform Application [closed]Webform应用程序中的Yahoo Weather API实现[关闭]
【发布时间】: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


【解决方案1】:

这一行

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

必须是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2019-06-04
    • 2018-02-04
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多