/// <summary> /// This function parsing the xml from the REStful Web Service site /// and set all the values. /// </summary> /// <param name="wd">The WeatherData object. </param> /// <param name="location">The requested location</param> public void XMLFunction(WeatherDataSite1 wd, Location location) { String URLString = "http://api.openweathermap.org/data/2.5/weather?q=" + location.Country + "&mode=xml"; string xml; using (WebClient client = new WebClient()) { try { xml = client.DownloadString(URLString);// xml url to string } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } } try { XDocument ob = XDocument.Parse(xml); //A linq to xml that get all the values from the site var weather = from x in ob.Descendants("current") select new { City = x.Descendants("city").Attributes("name").First().Value, Sun = x.Descendants("sun").Attributes("rise").First().Value, Set = x.Descendants("sun").Attributes("set").First().Value, Tempat = x.Descendants("temperature").Attributes("value").First().Value, Cloud = x.Descendants("clouds").Attributes("name").First().Value, Humidity = x.Descendants("humidity").Attributes("value").First().Value, Speed = x.Descendants("speed").Attributes("value").First().Value, Direction = x.Descendants("direction").Attributes("code").First().Value, Update = x.Descendants("lastupdate").Attributes("value").First().Value, }; //Get all the values from the linq vairables and set //them into the WeatherData service values. foreach (var data in weather) { wd.location.Country = data.City; wd.sunrise.Sunrise = data.Sun; wd.sunset.Sunset = data.Set; wd.lastupdate.Update = data.Update; wd.temperature.Celsius = Double.Parse(data.Tempat); wd.cloud.Clouds = data.Cloud; wd.humidity.Humitidy = data.Humidity; wd.wind.Speed = Double.Parse(data.Speed); wd.wind.Direction = data.Direction; } wd.temperature.KelvinToCelsius(); } catch(XmlException ) { throw new WeatherDataServiceException("Wrong Country"); } catch (WebException ) {
/// <summary> /// Default constructor that initialize all the instance variable /// </summary> public WeatherData() { location = new Location(); sunrise = new SunRise(); sunset = new SunSet(); cloud = new Cloud(); humidity = new Humidity(); lastupdate = new LastUpdate(); wind = new Wind(); temperature = new Temperature(); }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public override WeatherData getWeatherData(Location location) { WeatherDataSite2 wd = new WeatherDataSite2(); try { wd.location.Country = location.Country; XMLFunction(wd, location); } catch (WeatherDataServiceException ex) { Console.WriteLine(ex.Message); } return wd; }
public void StartParsing(Location location) { //TO DOO }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="wd">The WeatherData object. </param> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> public void XMLFunction(WeatherDataSite2 wd, Location location) { String URLString = "http://api.worldweatheronline.com/premium/v1/weather.ashx?key=" + key + "&q=" + location.Country + "&num_of_days=1&tp=24&format=xml"; string xml; using (WebClient client = new WebClient()) { try { xml = client.DownloadString(URLString);// xml url to string } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } } try { XDocument ob = XDocument.Parse(xml); //A linq to xml that get all the values from the site var weather = from x in ob.Descendants("data") select new { City = x.Descendants("query").First().Value, Ip = x.Descendants("type").First().Value, Sun = x.Descendants("sunrise").First().Value, Set = x.Descendants("sunset").First().Value, Tempat = x.Descendants("temp_C").First().Value, Cloud = x.Descendants("cloudcover").First().Value, Humidity = x.Descendants("humidity").First().Value, Speed = x.Descendants("windspeedKmph").First().Value, Direction = x.Descendants("winddir16Point").First().Value, Update = x.Descendants("date").First().Value, }; //Get all the values from the linq vairables and set //them into the WeatherData service values. foreach (var data in weather) { //This restful web service also support an ip search. //this check is to confrim that the user pressed a country. if(data.Ip=="IP") { throw new XmlException(); } wd.location.Country = data.City; wd.sunrise.Sunrise = data.Sun; wd.sunset.Sunset = data.Set; wd.lastupdate.Update = data.Update; wd.temperature.Celsius = Double.Parse(data.Tempat); wd.cloud.Clouds = data.Cloud; wd.humidity.Humitidy = data.Humidity; wd.wind.Speed = Double.Parse(data.Speed); wd.wind.Direction = data.Direction; } } catch (XmlException) { throw new WeatherDataServiceException("Wrong Country"); } catch (WebException) { throw new WeatherDataServiceException("There is not internet connection"); } catch (InvalidOperationException ex) { throw new WeatherDataServiceException(ex.Message); } }
/// <summary> /// A virtual method that get the weather object of a chosen country. /// </summary> /// <param name="location">Location requested by user to get service.</param> /// <returns>return a WeatherData object with all the params. </returns> virtual public WeatherData getWeatherData(Location location) { throw new NotImplementedException(); }