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();
}
}
}
}
}
`
Try updating your
GetFormattedXml
to be as following:and then you can rename it to
GetTemperature