Example #1
0
        /// <summary>
        /// Parse xml from openweathermap.org
        /// </summary>
        ForecastsList GetDataFromXml(XmlDocument doc)
        {
            doc.Save("forecast.xml");             //сохранять больше не требуется, но будем и дальше это делать для сущей красоты
            var result = new ForecastsList();

            result.ParseUnitsFromXml(doc);

            var elements = doc.SelectNodes("//time");

            //парсинг с помощью xpath
            foreach (XmlNode node in elements)
            {
                var forecast = new WeatherData();

                var timeFromValue = node.SelectSingleNode("@from").Value;
                var timeToValue   = node.SelectSingleNode("@to").Value;
                var timeFrom      = DateTime.Parse(timeFromValue);
                var timeTo        = DateTime.Parse(timeToValue);
                forecast.DateStr         = timeFrom.ToShortDateString();
                forecast.TimeStr         = timeFrom.ToShortTimeString() + " - " + timeTo.ToShortTimeString();
                forecast.Temperature     = /*float.Parse(*/ node.SelectSingleNode("temperature/@value").Value;     //);
                forecast.WindDirection   = node.SelectSingleNode("windDirection/@name").Value;
                forecast.WindSpeed       = /*float.Parse(*/ node.SelectSingleNode("windSpeed/@mps").Value;         //);
                forecast.WindDescription = node.SelectSingleNode("windSpeed/@name").Value;
                forecast.Pressure        = /*float.Parse(*/ node.SelectSingleNode("pressure/@value").Value;        //);
                var _precipitationType = node.SelectSingleNode("precipitation/@type");
                forecast.PrecipitationDescription = _precipitationType == null ? "" : _precipitationType.Value;
                if (forecast.PrecipitationDescription == "show")
                {
                    forecast.PrecipitationDescription = "snow";
                }
                var _precValue = /*float.Parse(*/ node.SelectSingleNode("precipitation/@value");               //);
                forecast.Precipitation     = _precValue == null ? "" : _precValue.Value;
                forecast.Humidity          = /*float.Parse(*/ node.SelectSingleNode("humidity/@value").Value;  //);
                forecast.CloudsDescription = node.SelectSingleNode("clouds/@value").Value;
                forecast.Clouds            = node.SelectSingleNode("clouds/@all").Value;
                result.Forecasts.Add(forecast);
            }
            return(result);
        }
Example #2
0
		/// <summary>
		/// Parse xml from openweathermap.org
		/// </summary>
		ForecastsList GetDataFromXml(XmlDocument doc) {
			doc.Save("forecast.xml"); //сохранять больше не требуется, но будем и дальше это делать для сущей красоты
			var result = new ForecastsList();
			result.ParseUnitsFromXml(doc);

			var elements = doc.SelectNodes("//time");

			//парсинг с помощью xpath
			foreach (XmlNode node in elements) {
				var forecast = new WeatherData();

				var timeFromValue = node.SelectSingleNode("@from").Value;
				var timeToValue = node.SelectSingleNode("@to").Value;
				var timeFrom = DateTime.Parse(timeFromValue);
				var timeTo = DateTime.Parse(timeToValue);
				forecast.DateStr = timeFrom.ToShortDateString();
				forecast.TimeStr = timeFrom.ToShortTimeString() + " - " + timeTo.ToShortTimeString();
				forecast.Temperature = /*float.Parse(*/node.SelectSingleNode("temperature/@value").Value;//);
				forecast.WindDirection = node.SelectSingleNode("windDirection/@name").Value;
				forecast.WindSpeed = /*float.Parse(*/node.SelectSingleNode("windSpeed/@mps").Value;//);
				forecast.WindDescription = node.SelectSingleNode("windSpeed/@name").Value;
				forecast.Pressure = /*float.Parse(*/node.SelectSingleNode("pressure/@value").Value;//);
				var _precipitationType = node.SelectSingleNode("precipitation/@type");
				forecast.PrecipitationDescription = _precipitationType == null ? "" : _precipitationType.Value;
				if (forecast.PrecipitationDescription == "show") {
					forecast.PrecipitationDescription = "snow";
				}
				var _precValue = /*float.Parse(*/node.SelectSingleNode("precipitation/@value");//);
				forecast.Precipitation = _precValue == null ? "" : _precValue.Value;
				forecast.Humidity = /*float.Parse(*/node.SelectSingleNode("humidity/@value").Value;//);
				forecast.CloudsDescription = node.SelectSingleNode("clouds/@value").Value;
				forecast.Clouds = node.SelectSingleNode("clouds/@all").Value;
				result.Forecasts.Add(forecast);
			}
			return result;
		}