private ForcustItem SerializeWeatherData(XmlNode data) { ForcustItem result = new ForcustItem(); XDocument doc = XDocument.Parse(data.OuterXml); if (doc.Root == null) { return(result); } result.CityName = doc.Root.Elements().Select(x => x.Element("name")).ElementAt(0).Value; var test = doc.Root.Elements().Where(e => e.Element("time") != null); foreach (var node in test.Elements()) { result.Forecast.Add(GetForecastFromXML(node)); } return(result); }
private ForcustItem GetCurrentWeatherfromXml(XmlNode data) { ForcustItem result = new ForcustItem(); XDocument doc = XDocument.Parse(data.OuterXml); if (doc.Root == null) { return(result); } //=================================/ Get Data from XmlDocument /============================================= var city = doc.Root.Elements().Where(x => x.Name == "city").Attributes("name"); var sTemperature = doc.Root.Elements().Where(x => x.Name == "temperature").Attributes("value").ElementAt(0).Value; var sHumidity = doc.Root.Elements().Where(x => x.Name == "humidity").Attributes("value").ElementAt(0).Value; var sPressure = doc.Root.Elements().Where(x => x.Name == "pressure").Attributes("value").ElementAt(0).Value; var sWindSpeed = doc.Root.Elements().Where(x => x.Name == "wind").Elements().Where(e => e.Name == "speed"); var sClouds = doc.Root.Elements().Where(x => x.Name == "clouds").Attributes("name").ElementAt(0).Value; var sPrecipitationType = doc.Root.Elements().Where(x => x.Name == "precipitation").Attributes("mode").ElementAt(0).Value; float precipitationValue = 0; if (doc.Root.Elements().Where(x => x.Name == "precipitation").Attributes("value").Any()) { var sPrecipitationValue = doc.Root.Elements().Where(x => x.Name == "precipitation").Attributes("value").ElementAt(0).Value; precipitationValue = float.Parse(sPrecipitationValue, CultureInfo.InvariantCulture.NumberFormat); } var sDate = doc.Root.Elements().Where(x => x.Name == "lastupdate").Attributes("value").ElementAt(0).Value; //=================================/ Convert Data /========================================================== var temperature = float.Parse(sTemperature, CultureInfo.InvariantCulture.NumberFormat); var humidity = float.Parse(sHumidity, CultureInfo.InvariantCulture.NumberFormat); var pressure = float.Parse(sPressure, CultureInfo.InvariantCulture.NumberFormat); var windSpeed = float.Parse(sWindSpeed.Attributes("value").ElementAt(0).Value, CultureInfo.InvariantCulture.NumberFormat); var clouds = sClouds; var precipitationType = sPrecipitationType; var date = Convert.ToDateTime(sDate); //=================================/ Add Data to ForcustItem object /====================================== result.CityName = city.ElementAt(0).Value; var temData = new Data() { From = date, To = date }; var temPrecipitation = new Precipitation() { Type = precipitationType, Value = precipitationValue }; var forecust = new ForeCast() { CloudsValue = clouds, Temperature = temperature, WindSpeed = windSpeed, Pressure = pressure, Humidity = humidity, Data = temData, Precipitation = temPrecipitation }; result.Forecast.Add(forecust); return(result); }