public static void Insert(MongoCollection<Weather> collection, Weather weather) { try { collection.CreateIndex(IndexKeys<Weather>.Descending(c => c.Date)); var result = (from c in collection.AsQueryable<Weather>() where c.Date == weather.Date select c) .Count(); if (result == 0) collection.Insert(weather); Console.WriteLine(weather); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private static Weather ParseWeather(string url) { WebClient webClient = new WebClient(); string webString = webClient.DownloadString(url); //Console.WriteLine(webString); Weather weather = new Weather(); weather.Date = DateTime.Now.Date; weather.location = new Location(); weather.units = new Units(); weather.wind = new Wind(); weather.atmosphere = new Atmosphere(); weather.astronomy = new Astronomy(); weather.condition = new Condition(); XmlDocument doc = new XmlDocument(); doc.LoadXml(webString); XmlNode node = doc.GetElementsByTagName("yweather:location")[0]; if (node == null) { return null; } weather.location.city = node.Attributes["city"].Value; weather.location.country = node.Attributes["country"].Value; weather.location.region = node.Attributes["region"].Value; node = doc.GetElementsByTagName("yweather:units")[0]; weather.units.distance = node.Attributes["distance"].Value; weather.units.pressure = node.Attributes["pressure"].Value; weather.units.speed = node.Attributes["speed"].Value; weather.units.temperature = node.Attributes["temperature"].Value; node = doc.GetElementsByTagName("yweather:wind")[0]; weather.wind.chill = ToDouble(node.Attributes["chill"].Value); weather.wind.direction = ToDouble(node.Attributes["direction"].Value); weather.wind.speed = ToDouble(node.Attributes["speed"].Value); node = doc.GetElementsByTagName("yweather:atmosphere")[0]; weather.atmosphere.humidity = ToDouble(node.Attributes["humidity"].Value); weather.atmosphere.pressure = ToDouble(node.Attributes["pressure"].Value); weather.atmosphere.rising = ToDouble(node.Attributes["rising"].Value); weather.atmosphere.visibility = ToDouble(node.Attributes["visibility"].Value); node = doc.GetElementsByTagName("yweather:astronomy")[0]; weather.astronomy.sunrise = node.Attributes["sunrise"].Value; weather.astronomy.sunset = node.Attributes["sunset"].Value; node = doc.GetElementsByTagName("yweather:condition")[0]; weather.condition.code = ToDouble(node.Attributes["code"].Value); weather.condition.date = node.Attributes["date"].Value; weather.condition.temp = ToDouble(node.Attributes["temp"].Value); weather.condition.text = node.Attributes["text"].Value; return weather; }