public void GetForecast(Int32 locationID, UnitsOfTemperature unit) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://weather.yahooapis.com/forecastrss?w=" + locationID.ToString() + (unit == UnitsOfTemperature.Celsius ? "&u=c" : "&u=f")); myReq.Method = "GET"; HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse(); Stream stream = myResp.GetResponseStream(); XDocument xmlResponse = XDocument.Load(stream); foreach (XElement el in xmlResponse.Descendants().Where(e => e.Name.LocalName == "forecast")) { this.LastUpdate = xmlResponse.Descendants().Where(e => e.Name.LocalName == "lastBuildDate").FirstOrDefault().Value; ForecastDay forecast = new ForecastDay(); forecast.Day = el.Attribute("day").Value; forecast.Date = el.Attribute("date").Value; forecast.Low = Int16.Parse(el.Attribute("low").Value); forecast.High = Int16.Parse(el.Attribute("high").Value); forecast.Description = el.Attribute("text").Value; forecast.Code = Int16.Parse(el.Attribute("code").Value); this._forecastDays.Add(forecast); } myResp.Close(); stream.Close(); }
/// <summary> /// Gets the current wheather information. /// </summary> /// <returns></returns> public void GetWeather(Int32 locationID, UnitsOfTemperature unit) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://weather.yahooapis.com/forecastrss?w=" + locationID.ToString() + (unit == UnitsOfTemperature.Celsius ? "&u=c" : "&u=f")); myReq.Method = "GET"; HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse(); Stream stream = myResp.GetResponseStream(); XDocument xmlResponse = XDocument.Load(stream); foreach (XElement el in xmlResponse.Root.Elements()) { this.LastUpdate = el.Descendants().Where(e => e.Name.LocalName == "lastBuildDate").FirstOrDefault().Value; XElement atm = el.Descendants().Where(e => e.Name.LocalName == "atmosphere").FirstOrDefault(); this.Humidity = Byte.Parse(atm.Attribute("humidity").Value); this.Pressure = Single.Parse(atm.Attribute("pressure").Value.Replace(".", ",")); this.Rising = (BarometricPressureState)Byte.Parse(atm.Attribute("rising").Value); XElement astronomy = el.Descendants().Where(e => e.Name.LocalName == "astronomy").FirstOrDefault(); this.Sunrise = astronomy.Attribute("sunrise").Value; this.Sunset = astronomy.Attribute("sunset").Value; XElement condition = el.Descendants().Where(e => e.Name.LocalName == "condition").FirstOrDefault(); this.Temp = Int16.Parse(condition.Attribute("temp").Value); this.Code = Int16.Parse(condition.Attribute("code").Value); this.Description = condition.Attribute("text").Value; } myResp.Close(); stream.Close(); }