コード例 #1
0
 private static WeatherReport ConvertToMetric(WeatherReport result)
 {
     result.LatestWeather.Temperature = 5.0/9.0*(result.LatestWeather.Temperature - 32.0);
     foreach (WeatherRange wr in result.Forecast)
     {
         wr.HighTemperature = 5.0/9.0*(wr.HighTemperature - 32.0);
         wr.LowTemperature = 5.0/9.0*(wr.LowTemperature - 32.0);
     }
     return result;
 }
コード例 #2
0
        private void GetLatestWeather(object sender, DoWorkEventArgs e)
        {
            string feedUrl = "http://weather.service.msn.com/data.aspx?src=vista&wealocations=" + _zipCode;

            var reader = new XmlTextReader(feedUrl);

            XDocument weather;
            try
            {
                weather = XDocument.Load(reader);
            }
            catch (Exception ex)
            {
                // TODO:  Initialize or throw an exception...
                return;
            }

            var weatherReport = new WeatherReport
                                    {
                                        Location = (from w in weather.Descendants("weather")
                                                    select new Location
                                                               {
                                                                   FullName =
                                                                       GetString(w.Attribute("weatherlocationname"))
                                                               }).FirstOrDefault(),
                                        Forecast = (from f in weather.Descendants("forecast")
                                                    select new WeatherRange
                                                               {
                                                                   HighTemperature = GetDouble(f.Attribute("high")),
                                                                   LowTemperature = GetDouble(f.Attribute("low")),
                                                                   Precipitation = GetDouble(f.Attribute("precip")),
                                                                   SkyCondition =
                                                                       {
                                                                           SkyCondition =
                                                                               GetString(f.Attribute("skytextday")),
                                                                           SkyImage = GetUri(f.Attribute("skycodeday"))
                                                                       },
                                                                   StartTime =
                                                                       GetDateTime(
                                                                       GetDateTime(f.Attribute("date")).ToString("o")),
                                                                   EndTime =
                                                                       GetDateTime(
                                                                       GetDateTime(f.Attribute("date")).ToString("o"))
                                                               }).Skip(1).Take(3).ToList(),
                                        LatestWeather = (from c in weather.Descendants("current")
                                                         select new WeatherPoint
                                                                    {
                                                                        Temperature =
                                                                            GetDouble(c.Attribute("temperature")),
                                                                        FeelsLike = GetDouble(c.Attribute("feelslike")),
                                                                        Humidity = GetDouble(c.Attribute("humidity")),
                                                                        Precipitation = GetDouble(c.Attribute("precip")),
                                                                        SkyCondition =
                                                                            {
                                                                                SkyCondition =
                                                                                    GetString(c.Attribute("skytext")),
                                                                                SkyImage =
                                                                                    GetUri(c.Attribute("skycode"))
                                                                            },
                                                                        WindVector =
                                                                            ParseSpeed(c.Attribute("winddisplay")),
                                                                        Time =
                                                                            ParseTime(c.Attribute("observationtime"),
                                                                                      c.Attribute("date"))
                                                                    }).FirstOrDefault()
                                    };

            var skyCode = (from c in weather.Descendants("current")
                           select new
                                      {
                                          SkyCode = GetInt(c.Attribute("skycode"))
                                      }).FirstOrDefault();

            weatherReport.SkyCode = skyCode.SkyCode;

            if (_unitsSystem == UnitsSystems.Metric)
            {
                weatherReport = ConvertToMetric(_weatherReport);
            }

            lock (_lock)
            {
                _weatherReport = weatherReport;
            }
        }