public static string GetWeather(string cityName) { string result = string.Empty; WeatherEnity weather = WeatherParser.GetCurrent(cityName); if (!weather.Error) { result = string.Format("Погода в: {0}, {1}{2}Температура: {3} °C", weather.CityName, weather.CountryName, Environment.NewLine, weather.Temperature); } return(result); }
public static WeatherEnity GetCurrent(string cityName) { WeatherEnity wn = new WeatherEnity(); string apiResult = CallRestMethod(UrlCurrent, cityName); if (String.IsNullOrEmpty(apiResult) || apiResult.Contains("Error")) { wn.Error = true; return wn; } XDocument doc = XDocument.Parse(apiResult); if (doc.Root != null) { IEnumerable<XElement> childList = doc.Root.Elements().ToList(); XElement cityNode = childList.FirstOrDefault(e => e.Name == "city"); if (cityNode != null) { wn.CityName = cityNode.Attribute("name").Value; XElement countryNode = cityNode.Elements().FirstOrDefault(el => el.Name == "country"); if (countryNode != null && !String.IsNullOrEmpty(countryNode.Value)) { wn.CountryName = countryNode.Value; } } XElement tempNode = childList.FirstOrDefault(e => e.Name == "temperature"); if (tempNode != null && !String.IsNullOrEmpty(tempNode.Attribute("value").Value)) { wn.Temperature = tempNode.Attribute("value").Value; } } return wn; }
public static WeatherEnity GetCurrent(string cityName) { WeatherEnity wn = new WeatherEnity(); string apiResult = CallRestMethod(UrlCurrent, cityName); if (String.IsNullOrEmpty(apiResult) || apiResult.Contains("Error")) { wn.Error = true; return(wn); } XDocument doc = XDocument.Parse(apiResult); if (doc.Root != null) { IEnumerable <XElement> childList = doc.Root.Elements().ToList(); XElement cityNode = childList.FirstOrDefault(e => e.Name == "city"); if (cityNode != null) { wn.CityName = cityNode.Attribute("name").Value; XElement countryNode = cityNode.Elements().FirstOrDefault(el => el.Name == "country"); if (countryNode != null && !String.IsNullOrEmpty(countryNode.Value)) { wn.CountryName = countryNode.Value; } } XElement tempNode = childList.FirstOrDefault(e => e.Name == "temperature"); if (tempNode != null && !String.IsNullOrEmpty(tempNode.Attribute("value").Value)) { wn.Temperature = tempNode.Attribute("value").Value; } } return(wn); }