public void LoadNews() { if (_myTimer == null) { _myTimer = new Timer(); _myTimer.Interval = 1000; _myTimer.Tick += MyTimerProcessor; _myTimer.Start(); } _lastLoad = DateTime.Now; _nextLoad = _lastLoad.AddMinutes((double)refreshUpDown.Value); label2.Text = _lastLoad.ToString("MMM-dd hh:mm tt"); label2.ForeColor = Color.Green; //_lastLoadError = null; _templist = new List <NewsEvent>(); try { // add a random query string to defeat server side caching. string urltweak = FfNewsUrl + "?x=" + Convert.ToString(DateTime.Now.Ticks); // ReSharper disable AccessToStaticMemberViaDerivedType HttpWebRequest newsReq = (HttpWebRequest)HttpWebRequest.Create(urltweak); // ReSharper restore AccessToStaticMemberViaDerivedType newsReq.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Reload); // fetch the xml doc from the web server using (HttpWebResponse newsResp = (HttpWebResponse)newsReq.GetResponse()) // check that we got a valid reponse if (newsResp != null && newsResp.StatusCode == HttpStatusCode.OK) { // read the response stream into and xml document Stream receiveStream = newsResp.GetResponseStream(); Encoding encode = Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader(receiveStream, encode); string xmlString = readStream.ReadToEnd(); XmlDocument newsDoc = new XmlDocument(); newsDoc.LoadXml(xmlString); // build collection of events int itemId = 0; if (newsDoc.DocumentElement != null) { foreach (XmlNode xmlNode in newsDoc.DocumentElement.ChildNodes) { NewsEvent newsEvent = new NewsEvent(); newsEvent.Time = xmlNode.SelectSingleNode("time").InnerText; if (string.IsNullOrEmpty(newsEvent.Time)) { continue; // ignore tentative events! } newsEvent.Date = xmlNode.SelectSingleNode("date").InnerText; // assembly and convert event date/time to local time. newsEvent.DateTimeLocal = DateTime.SpecifyKind(DateTime.Parse(newsEvent.Date + " " + newsEvent.Time, _ffDateTimeCulture), DateTimeKind.Utc).ToLocalTime(); // filter events based on settings... DateTime startTime = DateTime.Now; DateTime endTime = startTime.AddDays(1); // filter news events based on various property settings... if (newsEvent.DateTimeLocal < startTime || (_todaysNewsOnly && newsEvent.DateTimeLocal.Date >= endTime.Date)) { continue; } newsEvent.ID = ++itemId; newsEvent.Country = xmlNode.SelectSingleNode("country").InnerText; if (!checkedListBoxCountries.CheckedItems.Contains(newsEvent.Country.ToUpper()) && newsEvent.Country.ToUpper() != "ALL") { continue; } newsEvent.Forecast = xmlNode.SelectSingleNode("forecast").InnerText; newsEvent.Impact = xmlNode.SelectSingleNode("impact").InnerText; if ((!_impactLow && newsEvent.Impact.ToUpper() == "LOW") || (!_impactMedium && newsEvent.Impact.ToUpper() == "MEDIUM") || (!_impactHigh && newsEvent.Impact.ToUpper() == "HIGH")) { continue; } newsEvent.Previous = xmlNode.SelectSingleNode("previous").InnerText; newsEvent.Title = xmlNode.SelectSingleNode("title").InnerText; newsEvent.AlertFired = false; newsEvent.AlertChecked = false; _templist.Add(newsEvent); } } ReconcileLists(); PopulateGlacialList(_list); } else // handle unexpected scenarios... if (newsResp == null) { throw new Exception("Web response was null."); } else { throw new Exception(string.Format("Web response status code = {0}", newsResp.StatusCode)); } } catch (Exception) { //_lastLoadError = ex.Message; label2.Text = "Unsuccesful"; label2.ForeColor = Color.Red; } }
public void LoadNews() { if (_myTimer == null) { _myTimer = new Timer(); _myTimer.Interval = 1000; _myTimer.Tick += MyTimerProcessor; _myTimer.Start(); } _lastLoad = DateTime.Now; _nextLoad = _lastLoad.AddMinutes((double)refreshUpDown.Value); label2.Text = _lastLoad.ToString("MMM-dd hh:mm tt"); label2.ForeColor = Color.Green; //_lastLoadError = null; _templist = new List<NewsEvent>(); try { // add a random query string to defeat server side caching. string urltweak = FfNewsUrl + "?x=" + Convert.ToString(DateTime.Now.Ticks); // ReSharper disable AccessToStaticMemberViaDerivedType HttpWebRequest newsReq = (HttpWebRequest)HttpWebRequest.Create(urltweak); // ReSharper restore AccessToStaticMemberViaDerivedType newsReq.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Reload); // fetch the xml doc from the web server using (HttpWebResponse newsResp = (HttpWebResponse)newsReq.GetResponse()) // check that we got a valid reponse if (newsResp != null && newsResp.StatusCode == HttpStatusCode.OK) { // read the response stream into and xml document Stream receiveStream = newsResp.GetResponseStream(); Encoding encode = Encoding.GetEncoding("utf-8"); StreamReader readStream = new StreamReader(receiveStream, encode); string xmlString = readStream.ReadToEnd(); XmlDocument newsDoc = new XmlDocument(); newsDoc.LoadXml(xmlString); // build collection of events int itemId = 0; if (newsDoc.DocumentElement != null) foreach (XmlNode xmlNode in newsDoc.DocumentElement.ChildNodes) { NewsEvent newsEvent = new NewsEvent(); newsEvent.Time = xmlNode.SelectSingleNode("time").InnerText; if (string.IsNullOrEmpty(newsEvent.Time)) continue; // ignore tentative events! newsEvent.Date = xmlNode.SelectSingleNode("date").InnerText; // assembly and convert event date/time to local time. newsEvent.DateTimeLocal = DateTime.SpecifyKind(DateTime.Parse(newsEvent.Date + " " + newsEvent.Time, _ffDateTimeCulture), DateTimeKind.Utc).ToLocalTime(); // filter events based on settings... DateTime startTime = DateTime.Now; DateTime endTime = startTime.AddDays(1); // filter news events based on various property settings... if (newsEvent.DateTimeLocal < startTime || (_todaysNewsOnly && newsEvent.DateTimeLocal.Date >= endTime.Date)) continue; newsEvent.ID = ++itemId; newsEvent.Country = xmlNode.SelectSingleNode("country").InnerText; if (!checkedListBoxCountries.CheckedItems.Contains(newsEvent.Country.ToUpper()) && newsEvent.Country.ToUpper() != "ALL") continue; newsEvent.Forecast = xmlNode.SelectSingleNode("forecast").InnerText; newsEvent.Impact = xmlNode.SelectSingleNode("impact").InnerText; if ((!_impactLow && newsEvent.Impact.ToUpper() == "LOW") || (!_impactMedium && newsEvent.Impact.ToUpper() == "MEDIUM") || (!_impactHigh && newsEvent.Impact.ToUpper() == "HIGH")) continue; newsEvent.Previous = xmlNode.SelectSingleNode("previous").InnerText; newsEvent.Title = xmlNode.SelectSingleNode("title").InnerText; newsEvent.AlertFired = false; newsEvent.AlertChecked = false; _templist.Add(newsEvent); } ReconcileLists(); PopulateGlacialList(_list); } else // handle unexpected scenarios... if (newsResp == null) throw new Exception("Web response was null."); else throw new Exception(string.Format("Web response status code = {0}", newsResp.StatusCode)); } catch (Exception) { //_lastLoadError = ex.Message; label2.Text = "Unsuccesful"; label2.ForeColor = Color.Red; } }