Read xml from URL
本文关键字:URL from xml Read | 更新日期: 2023-09-27 17:59:30
这就是我目前所拥有的。我正试图从URL中读取XML,并获取例如温度、湿度。。。。等等……但每次我尝试其他东西时,都会出错。我想检索信息并将其放入标签中。
namespace WindowsFormsApplication1 {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e) {
String zip = txtZip.Text;
XmlDocument weatherURL = new XmlDocument();
weatherURL.Load("http://api.wunderground.com/api/"
your_key "/conditions/q/" + zip + ".xml");
foreach(XmlNode nodeselect in weatherURL.SelectNodes("response/current_observation"));
}
}
}
这花了我一些尝试和错误,但我已经做到了。在C#中,请确保您使用的是使用System.Xml的-
下面是使用神童API的代码。为了让它发挥作用,请确保你注册了一个密钥,否则它不会起作用。这是你的钥匙,也就是你把钥匙放在哪里。它应该看起来像这样。我用了一个按钮和三个标签来显示信息。
namespace wfats2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc1 = new XmlDocument();
doc1.Load("http://api.wunderground.com/api/your_key/conditions/q/92135.xml");
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/response/current_observation");
foreach (XmlNode node in nodes)
{
string tempf = node["temp_f"].InnerText;
string tempc = node["temp_c"].InnerText;
string feels = node["feelslike_f"].InnerText;
label2.Text = tempf;
label4.Text = tempc;
label6.Text = feels;
}
}
}
}
当您按下按钮时,您将获得显示在分配标签中的信息。我仍在试验,你可以每隔一段时间进行某种刷新,而不是每次都按下按钮来获得更新。
首先,是的,你需要在问题中提供更多信息,但我马上就能看到你的URL中有"your_key"。您可能需要将其替换为API密钥才能正常工作。