コード例 #1
0
        /// <summary>
        /// Return comparing results
        /// </summary>
        public bool Equals(WeatherData wd)
        {
            // If parameter is null return false:
            if ((object)wd == null)
            {
                return false;
            }

            // Return true if the fields match:
            return ((this.Clouds.Equals(wd.Clouds, StringComparison.Ordinal)) && (this.Time.Equals(wd.Time)) &&
                (this.Loc.Equals(wd.Loc)) && (this.Temperature.Equals(wd.Temperature)));
        }
コード例 #2
0
        /// <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;
        }