Beispiel #1
0
        /// <summary>
        /// Responsible to parse the XML and translates it to Location and WeatherData objects.
        /// It also initialize the location object with all relevant details.
        /// </summary>
        /// <param name="location"></param>
        /// <exception cref="WeatherDataServiceException">
        /// Thrown when method fail to initialize Location and Weatherdata from XML.</exception>
        /// <returns>WeatherData object with all relevant information</returns>
        private WeatherData ParseXmlToWeatherData(Location location)
        {
            WebClient client = null;

            try
            {
                client = new WebClient();
                string    xml = @client.DownloadString(UrlXmlAddress.ToString());
                XDocument doc = XDocument.Parse(xml);
                location.Initialize                     //fill out all missing data of location object
                (
                    doc.Descendants("city").Attributes("name").First().Value,
                    double.Parse(doc.Descendants("coord").Attributes("lon").First().Value),
                    double.Parse(doc.Descendants("coord").Attributes("lat").First().Value),
                    doc.Descendants("country").First().Value
                );
                return(new WeatherData                  //return WeatherData object with full data inside
                       (
                           location,
                           double.Parse(doc.Descendants("temperature").Attributes("value").First().Value),
                           double.Parse(doc.Descendants("humidity").Attributes("value").First().Value),
                           double.Parse(doc.Descendants("pressure").Attributes("value").First().Value),
                           doc.Descendants("clouds").Attributes("name").First().Value,
                           doc.Descendants("lastupdate").Attributes("value").First().Value,
                           double.Parse(doc.Descendants("speed").Attributes("value").First().Value) * 3.6,
                           doc.Descendants("speed").Attributes("name").First().Value,
                           doc.Descendants("direction").Attributes("name").First().Value
                       ));
            }
            catch (WebException)
            {
                throw new WeatherDataServiceException("Faild to connect to openweathermap.org");
            }
            catch (XmlException)
            {
                throw new WeatherDataServiceException("Failed to find the XML document");
            }
            catch (InvalidOperationException)
            {
                throw new WeatherDataServiceException("Failed to parse the XML");
            }
            catch (FormatException)
            {
                throw new WeatherDataServiceException("Failed to format the string to a number");
            }
            catch (Exception e)
            {
                throw new WeatherDataServiceException(e.Message);
            }
            finally
            {
                //Closing the connection to openweathermap.org
                client?.Dispose();

                //Restarting the address so next time we also get a correct url address
                UrlXmlAddress.Clear();
            }
        }
        /// <summary>
        /// Responsible to parse the Json and translates it to Location and WeatherData objects.
        /// It also initialize the location object with all relevant details.
        /// </summary>
        /// <param name="location"></param>
        /// <exception cref="WeatherDataServiceException">
        /// Thrown when method fail to initialize Location and Weatherdata from Json.</exception>
        /// <returns>WeatherData object with all relevant information</returns>
        private WeatherData ParseJsonToWeatherData(Location location)
        {
            WebClient    client          = null;
            const string windDescription = "degree is ";

            try
            {
                client = new WebClient();
                string  json       = @client.DownloadString(UrlJsonAddress.ToString());
                JObject jsonObject = JObject.Parse(json);
                location.Initialize
                (
                    (string)jsonObject["location"]["name"],
                    double.Parse((string)jsonObject["location"]["lon"]),
                    double.Parse((string)jsonObject["location"]["lat"]),
                    (string)jsonObject["location"]["country"]
                );
                return(new WeatherData
                       (
                           location,
                           double.Parse((string)jsonObject["current"]["temp_c"]),
                           double.Parse((string)jsonObject["current"]["humidity"]),
                           double.Parse((string)jsonObject["current"]["pressure_mb"]),
                           ((string)jsonObject["current"]["condition"]["text"]).Remove(((string)jsonObject["current"]["condition"]["text"]).Length - 1),
                           (string)jsonObject["current"]["last_updated"],
                           double.Parse((string)jsonObject["current"]["wind_kph"]),
                           windDescription.Insert(windDescription.Length, (string)jsonObject["current"]["wind_degree"]),
                           (string)jsonObject["current"]["wind_dir"]
                       ));
            }
            catch (WebException)
            {
                throw new WeatherDataServiceException("Faild to connect to www.apixu.com");
            }
            catch (FormatException)
            {
                throw new WeatherDataServiceException("Failed to format the string to a number");
            }
            catch (Exception e)
            {
                throw new WeatherDataServiceException(e.Message);
            }
            finally
            {
                //Closing the connection to openweathermap.org
                client?.Dispose();

                //Restarting the address so next time we also get a correct url address
                UrlJsonAddress.Clear();
            }
        }