C# Yahoo Weather API在Webform应用程序中的实现

C# Yahoo Weather API在Webform应用程序中的实现,c#,visual-studio,webforms,visual-studio-2013,C#,Visual Studio,Webforms,Visual Studio 2013,所以我试着跟随这段视频并实现Yahoo!在Visual Basic 2013 pro中将天气API(XML)导入我的Web表单项目 但是当我在调试模式下运行项目时,我得到了一个NullReferenceException错误 以下是截图- 还有我遇到问题的代码块- private void Getweather() { string query = String.Format("http://weather.yahooapis.com/forecastrss?w=4

所以我试着跟随这段视频并实现Yahoo!在Visual Basic 2013 pro中将天气API(XML)导入我的Web表单项目

但是当我在调试模式下运行项目时,我得到了一个
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;
    }
最后是

对编码来说,这是一个全新的概念。。感谢您的帮助!:)

这条线

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

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

在引发异常的行上设置一个断点,然后查看空值。可能是
。选择SingleNode(“yweather:condition”,manager)
@PoweredByOrange谢谢。。。它是!但是我该怎么解决这个问题呢?有什么想法吗?我想您错过了getWeather()中条件变量的第二个SelectSingleNode部分。看看你的链接视频again@envyM6尝试
频道。选择SingleNode(“项目”)。选择SingleNode(“Y天气:条件”,管理器)。属性[“文本”]。值
您缺少
项目
节点。有没有建议如何修改此代码以手动输入位置?