C# how to show only one entry in a xml document?

Question

I have a problem with my code: I want only to show the temperature from the XML returned from http://openweathermap.org/

namespace SMirror
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblWeather.Text = GetFormattedXml(CurrentUrl);
        }

        private const string API_KEY = "8b1ed1ebf1481ecf201d05b2feeca87d";
        private const string CurrentUrl = 
            "http://api.openweathermap.org/data/2.5/weather?" +
            "q=Berlin&mode=xml&units=imperial&APPID=" + API_KEY;
        private const string ForecastUrl =
            "http://api.openweathermap.org/data/2.5/forecast?" +
            "q=Berlin&mode=xml&units=imperial&APPID=" + API_KEY;

        // Return the XML result of the URL.
        private string GetFormattedXml(string url)
        {
            // Create a web client.
            using (WebClient client = new WebClient())
            {
                // Get the response string from the URL.
                string xml = client.DownloadString(url);

                // Load the response into an XML document.
                XmlDocument xml_document = new XmlDocument();
                xml_document.LoadXml(xml);

                // Format the XML.
                using (StringWriter string_writer = new StringWriter())
                {
                    XmlTextWriter xml_text_writer =
                    new XmlTextWriter(string_writer);
                    xml_text_writer.Formatting = Formatting.Indented;
                    xml_document.WriteTo(xml_text_writer);

                    // Return the result.
                    return string_writer.ToString();
                }
            }
        }
    }
}

`


Show source
| c#   | xml   | winforms   | weather   2017-01-03 23:01 2 Answers

Answers to C# how to show only one entry in a xml document? ( 2 )

  1. 2017-01-03 23:01

    Try updating your GetFormattedXml to be as following:

        private string GetFormattedXml(string url)
        {
            // Create a web client.
            using (WebClient client = new WebClient())
            {
                // Get the response string from the URL.
                string xml = client.DownloadString(url);
    
                // Load the response into an XML document.
                XmlDocument xml_document = new XmlDocument();
                xml_document.LoadXml(xml);
    
                return xml_document.SelectSingleNode("/current/temperature").Attributes["value"].Value;
            }
        }
    

    and then you can rename it to GetTemperature

  2. 2017-01-03 23:01

    You can use an XPath query to select the temperature node, and then get the value of the value element.

    XmlNode node = xml_document.DocumentElement.SelectSingleNode("/current/temperature");
    string current_temperature = node.Attributes.GetNamedItem("value").Value;
    

Leave a reply to - C# how to show only one entry in a xml document?

◀ Go back