I'm trying to make a simple app in C# that gets the weather data from http://openweathermap.org/api (OWM).
Here attached are two classes that I use to initialize WeatherData for selected City and to download and parse XML data from OWM API. I would like to know your opinion on the structure of the classes and the way I use them, because now I'm really not sure how should it work assuming that I would like to get/print more weather data in future.
I use them as follows:
WeatherData WarsawWeather = new WeatherData("Warsaw");
WarsawWeather.CheckWeather();
System.Console.WriteLine(WarsawWeather.Temp);
Classes:
using System.Net;
using System.Xml;
namespace WeatherApp
{
class WeatherData
{
public WeatherData(string City)
{
city = City;
}
private string city;
private float temp;
private float tempMax;
private float tempMin;
public void CheckWeather()
{
WeatherAPI DataAPI = new WeatherAPI(City);
temp = DataAPI.GetTemp();
}
public string City { get => city; set => city = value; }
public float Temp { get => temp; set => temp = value; }
public float TempMax { get => tempMax; set => tempMax = value; }
public float TempMin { get => tempMin; set => tempMin = value; }
}
class WeatherAPI
{
public WeatherAPI(string city)
{
SetCurrentURL(city);
xmlDocument = GetXML(CurrentURL);
}
public float GetTemp()
{
XmlNode temp_node = xmlDocument.SelectSingleNode("//temperature");
XmlAttribute temp_value = temp_node.Attributes["value"];
string temp_string = temp_value.Value;
return float.Parse(temp_string);
}
private const string APIKEY = "API KEY HERE";
private string CurrentURL;
private XmlDocument xmlDocument;
private void SetCurrentURL(string location)
{
CurrentURL = "http://api.openweathermap.org/data/2.5/weather?q="
+ location + "&mode=xml&units=metric&APPID=" + APIKEY;
}
private XmlDocument GetXML(string CurrentURL)
{
using (WebClient client = new WebClient())
{
string xmlContent = client.DownloadString(CurrentURL);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlContent);
return xmlDocument;
}
}
}
}
Example XML from OWM:
<current>
<city id="756135" name="Warsaw">
<coord lon="21.01" lat="52.23"/>
<country>PL</country>
<sun rise="2016-12-27T06:45:16" set="2016-12-27T14:30:06"/>
</city>
<temperature value="2" min="2" max="2" unit="metric"/>
<humidity value="80" unit="%"/>
<pressure value="1019" unit="hPa"/>
<wind>
<speed value="6.7" name="Moderate breeze"/>
<gusts/>
<direction value="310" code="NW" name="Northwest"/>
</wind>
<clouds value="40" name="scattered clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="802" value="scattered clouds" icon="03n"/>
<lastupdate value="2016-12-27T20:00:00"/>
</current>
public float TempMax { get => tempMax; set => tempMax = value; }
What is this syntax ? – denis Dec 27 '16 at 21:25