Read xml from url

This is what I have so far. I am trying to just read XML from a url and just get e.g. temperature, humidity ... etc. But every time I try something else it gives me an error. I want to get 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"));
        }
    }
}

      

+3


source to share


2 answers


It took me a bit of trial and error, but I got it. In C #, make sure you are using - using System.Xml;

Here is the code using the wunderground API. For this to work, make sure you subscribe to a different key key, but that won't work. Where they say it is your_key where you paste your key. It should look something like this. I used a button and three shortcuts to display 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 receive the information displayed in the label assignment. I am still experimenting and you can update every time as often as opposed to pressing a button every time to get the update.

+12


source


First of all, you need to give more information in your question, but I can see that you have "your_key" inside your url. You probably need to replace this with your API key for this to work.



0


source







All Articles