从读XML网址 [英] Read xml from URL
问题描述
这是我迄今。我想刚刚从URL读取XML和刚刚获得例如温度,湿度....等....但每次我试试别的时候它给了我一个错误。我想要检索的信息,并把它放在一个标签
命名空间WindowsFormsApplication1 {
公共部分Form1类:表格{
公共Form1的(){
的InitializeComponent();
}
私人无效btnSubmit_Click(对象发件人,EventArgs五){
字符串邮编= txtZip.Text;
XmlDocument的weatherURL =新的XmlDocument();
weatherURL.Load(http://api.wunderground.com/api/
your_key/环境/ Q /+拉链+的.xml);
的foreach(在weatherURL.SelectNodes XmlNode的nodeselect(响应/ current_observation));
}
}
}
我花了一点尝试和错误的,但我得到了它。在C#确保您使用的是 - 的使用的System.Xml; 的
下面是一个使用wunderground API的代码。为了这个工作,确保您注册一个关键的其他明智的,它不会工作。哪里是说这your_key那就是你把你的钥匙。它看起来应该是这样的。我用了一个按钮,三个标签来显示信息。
命名空间wfats2
{
公共部分Form1类:形式
{
公共Form1中()
{
的InitializeComponent();
}
私人无效的button1_Click(对象发件人,EventArgs五)
{
XmlDocument的DOC1 =新的XmlDocument();
doc1.Load(http://api.wunderground.com/api/your_key/conditions/q/92135.xml);
的XmlElement根= doc1.DocumentElement;
XmlNodeList中的节点= root.SelectNodes(/响应/ current_observation);
的foreach(在节点XmlNode的节点)
{
串tempf =节点[temp_f]的InnerText。
串tempc =节点[temp_c]的InnerText。
串感觉=节点[feelslike_f]的InnerText。
label2.Text = tempf;
label4.Text = tempc;
label6.Text =感觉;
}
}
}
}
当你按下按钮,你会得到分配的标签显示的信息。我仍然尝试和你能有某种刷新的每所以往往不是每次都得到更新按下按钮。
This is what I have so far. I am trying to just read the XML from the URL and just get for example temperature, humidity....etc.... But every time I try something else it gives me an error. I want to retrieve the information and put it in a label.
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"));
}
}
}
It took me a bit of trial and error but I've got it. In C# make sure you are using - using System.Xml;
Here is the code using wunderground API. In order for this to work make sure you sign up for a key other wise it will not work. Where is say this your_key that is where you put in your key. It should look like something like this. I used a button and three labels to display the information.
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;
}
}
}
}
When you press the button you will get the information displayed in the labels assign. I am still experimenting and you are able to have some sort of refresh every so often instead of pressing the button every time to get an update.
这篇关于从读XML网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!