/// <summary> /// The class constructor. /// </summary> public WeatherData(Location loc, Time time, Temperature temp, string clouds) { this.Loc = loc; this.Time = time; this.Temperature = temp; this.Clouds = clouds; }
/// <summary> /// Return comparing results /// </summary> public bool Equals(Time time) { // If parameter is null return false: if ((object)time == null) { return false; } // Return true if the fields match: return ((this.From.Equals(time.From, StringComparison.Ordinal)) && (this.To.Equals(time.To, StringComparison.Ordinal))); }
/// <summary> /// Parsing of weather data service XML - returns WeatherData object with all weather details /// </summary> private WeatherData ParseWeatherDataXml(string url) { XDocument xdoc = null; WeatherData wd = null; try { //getting xml document from url string xml = new WebClient().DownloadString(url); xdoc = XDocument.Parse(xml); var locNode = xdoc.Element("weatherdata").Element("location"); var timeNode = xdoc.Element("weatherdata").Element("forecast").Element("time"); //getting location values from xml Location location = new Location(locNode.Element("name").Value, locNode.Element("country").Value); //getting time values from xml Time time = new Time(timeNode.Attribute("from").Value, timeNode.Attribute("to").Value); //getting clouds description from xml string clouds = timeNode.Element("clouds").Attribute("value").Value; //getting temperature description from xml Temperature temp = new Temperature(timeNode.Element("temperature").Attribute("unit").Value, Convert.ToDouble(timeNode.Element("temperature").Attribute("value").Value), Convert.ToDouble(timeNode.Element("temperature").Attribute("min").Value), Convert.ToDouble(timeNode.Element("temperature").Attribute("max").Value)); //construct WeatherData object with all data wd = new WeatherData(location, time, temp, clouds); } catch (System.Net.WebException e) { throw new WeatherDataServiceException("Problem with downloading xml", e); } catch (System.Xml.XmlException e) { throw new WeatherDataServiceException("Problem with parsing xml", e); } catch (System.NullReferenceException e) { throw new WeatherDataServiceException("Element/Attribute name is incorrect", e); } return wd; }
private WeatherData GetWeatherDataForTest(string url) { string xml = new WebClient().DownloadString(url); XDocument xdoc = XDocument.Parse(xml); //getting values from xml var path = xdoc.Element("weatherdata").Element("location"); Location location = new Location(path.Element("name").Value, path.Element("country").Value); path = xdoc.Element("weatherdata").Element("forecast").Element("time"); Time time = new Time(path.Attribute("from").Value, path.Attribute("to").Value); string clouds = path.Element("clouds").Attribute("value").Value; Temperature temp = new Temperature(path.Element("temperature").Attribute("unit").Value, Convert.ToDouble(path.Element("temperature").Attribute("value").Value), Convert.ToDouble(path.Element("temperature").Attribute("min").Value), Convert.ToDouble(path.Element("temperature").Attribute("max").Value)); return new WeatherData(location, time, temp, clouds); }