C#带有雅虎天气api
我的意图是开发一个C#form应用程序,从yahoo weather api检索天气数据。我需要该程序从雅虎获取天气数据,并在相应的文本输入检索。代码如下C#带有雅虎天气api,c#,xml,yahoo-weather-api,C#,Xml,Yahoo Weather Api,我的意图是开发一个C#form应用程序,从yahoo weather api检索天气数据。我需要该程序从雅虎获取天气数据,并在相应的文本输入检索。代码如下 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Win
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Web;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace weather
{
public partial class Form1 : Form
{
string Temperature;
string Condition;
string Humidity;
string WindSpeed;
string Town;
string TFCond;
string TFHigh;
string TFLow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void GetWeather()
{
string query = String.Format("http://weather.yahooapis.com/forecastrss?w=1319153");
//string query = String.Format("http://weather.yahooapis.com/forecastrss?w=2502265");
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("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;
Humidity = channel.SelectSingleNode("yweather:atmosphere", manager).Attributes["humidity"].Value;
WindSpeed = channel.SelectSingleNode("yweather:wind", manager).Attributes["speed"].Value;
Town = channel.SelectSingleNode("yweather:location", 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)
{
textBox1.AppendText(Temperature);
textBox2.AppendText(Humidity);
}
}
}
gui如下所示
我需要善良的程序员的帮助 您定义了温度和湿度变量,但从未分配它们,因此它们实际上只是空值。您需要使用GetWeather()方法。您刚刚定义了此方法,但从未使用过它,因此您的温度和湿度变量仍然为空 在按钮单击的EventHandler中,您需要使用GetWeather方法
private void button1_Click(object sender, EventArgs e)
{
GetWeather();
textBox1.AppendText(Temperature);
textBox2.AppendText(Humidity);
}
您定义了温度和湿度变量,但从未分配它们,所以它们实际上只是空值。您需要使用GetWeather()方法。您刚刚定义了此方法,但从未使用过它,因此您的温度和湿度变量仍然为空 在按钮单击的EventHandler中,您需要使用GetWeather方法
private void button1_Click(object sender, EventArgs e)
{
GetWeather();
textBox1.AppendText(Temperature);
textBox2.AppendText(Humidity);
}
在调试器中运行此操作时,在哪一行引发
NullReferenceException
异常?请阅读异常:textBox1
和/或textBox2
为null
。也可能是温度
或湿度
都是空的
-但可能性较小。@John3136重新阅读此代码,并再次告诉我温度或湿度“不太可能”为空。它们肯定是空的。@MajkeloDev我没有阅读全部代码-我可以通过例外判断出这4个东西中的一个是空的。根据我的经验,通常控件本身是空的,但这并不总是正确的-就像在本例中一样。(如果我正在编写API,我会将null arg作为NOP处理)当您在调试器中运行此操作时,抛出的NullReferenceException
在哪一行?请阅读异常:textBox1
和/或textBox2
是null
。也可能是温度
或湿度
都是空的
-但可能性较小。@John3136重新阅读此代码,并再次告诉我温度或湿度“不太可能”为空。它们肯定是空的。@MajkeloDev我没有阅读全部代码-我可以通过例外判断出这4个东西中的一个是空的。根据我的经验,通常控件本身是空的,但这并不总是正确的-就像在本例中一样。(如果我正在编写API,我会将空参数作为NOP处理)感谢您的支持。。我很抱歉在这里问这种愚蠢的问题。如果有帮助的话-马克作为答案。很高兴我能帮忙。非常感谢。也许你需要将华氏温度转换为摄氏温度(F-32)/100,你还需要一个天气条件代码列表数组。名单上的老一个在这里谢谢你的支持。。我很抱歉在这里问这种愚蠢的问题。如果有帮助的话-马克作为答案。很高兴我能帮忙。非常感谢。也许你需要将华氏温度转换为摄氏温度(F-32)/100,你还需要一个天气条件代码列表数组。列表中的一个旧的在这里