//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if ( location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.wunderground.com/api/" + Key + "/conditions/q/" + location.countryName + "/" + location.cityName + ".xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("response") select new { Name = item.Element("current_observation").Element("display_location").Element("city").Value, Temp = item.Element("current_observation").Element("temp_c").Value, Pressure = item.Element("current_observation").Element("pressure_mb").Value, Humidity = item.Element("current_observation").Element("relative_humidity").Value, WindSpeed = item.Element("current_observation").Element("wind_kph").Value, WindDirection = item.Element("current_observation").Element("wind_dir").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = double.Parse(item.Temp); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity; WD.windSpeed = double.Parse(item.WindSpeed); WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from parsing error or wrong values in the Location"); } return WD; }
//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if (location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.openweathermap.org/data/2.5/weather?q=" + location.cityName + "," + location.countryName + "&mode=xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("current") select new { Name = item.Element("city").Attribute("name").Value, Temp = item.Element("temperature").Attribute("value").Value, Pressure = item.Element("pressure").Attribute("value").Value, Humidity = item.Element("humidity").Attribute("value").Value, WindSpeed = item.Element("wind").Element("speed").Attribute("value").Value, WindDirection = item.Element("wind").Element("direction").Attribute("code").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = FarToCell(double.Parse(item.Temp)); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity + "%"; WD.windSpeed = double.Parse(item.WindSpeed) * 3.6; //convert from mps to kph WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from internet problem or wrong values in the Location"); } return WD; }
//address to the site and building weatherdata sturcture public WeatherData GetWeatherData(Location location) { XDocument xdoc=null; WeatherData WD=new WeatherData(); string addr=""; try { if (location.countryName == null || location.cityName == null) { throw (new WeatherDataServiceException("WeatherDataServiceException : location.countryName or location.countryName are null")); } //sending a correct sturcure of query string to the site addr = "http://api.worldweatheronline.com/free/v2/weather.ashx?key=" + Key + "&q=" + location.cityName + ","+ location.countryName + "&format=xml"; xdoc = XDocument.Load(addr); } catch (Exception e) { Console.WriteLine(e.Message); } try { //parsing the XML recieved from the site by its unique stucture var list = from item in xdoc.Descendants("data") select new { Name = item.Element("request").Element("query").Value, Temp = item.Element("current_condition").Element("temp_C").Value, Pressure = item.Element("current_condition").Element("pressure").Value, Humidity = item.Element("current_condition").Element("humidity").Value, WindSpeed = item.Element("current_condition").Element("windspeedKmph").Value, WindDirection = item.Element("current_condition").Element("winddir16Point").Value }; foreach (var item in list) { //building the weatherdata structure WD.cityName = item.Name; WD.temp = double.Parse(item.Temp); WD.pressure = int.Parse(item.Pressure); WD.humidity = item.Humidity + "%"; WD.windSpeed = double.Parse(item.WindSpeed); WD.windDirection = item.WindDirection; } } catch (Exception e) { Console.WriteLine(e.Message + " : caused from internet problem or wrong values in the Location"); } return WD; }