Read xml from URL

前端 未结 2 1006
忘掉有多难
忘掉有多难 2020-12-06 08:49

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 g

相关标签:
2条回答
  • 2020-12-06 09:03

    First off yeah you need to give more information in your question but off hand I can see that you have "your_key" inside of your URL. You are probably needing to replace that with your API key for this to work.

    0 讨论(0)
  • 2020-12-06 09:04

    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.

    0 讨论(0)
提交回复
热议问题