private void parse_RecallListByCategory(object sender, HtmlDocumentLoadCompleted e) { IList < ProductViewModel > tmpItems = new List<ProductViewModel>(); IList<HtmlNode> hnc = e.Document.DocumentNode.DescendantNodes().ToList(); String currentCategory = null; Boolean flag_comment = false; foreach (HtmlNode node in hnc) { if (!flag_comment && node.Name.ToLower() == "#comment") { flag_comment = true; } else if (flag_comment && node.PreviousSibling != null && node.PreviousSibling.Name == "#comment") { flag_comment = false; } if (!flag_comment && node.Name.ToLower() == "h3") { currentCategory = node.InnerText; } else if (!flag_comment && node.Name.ToLower() == "li") { if (node.ParentNode.Name == "ul" && node.ParentNode.Attributes.Count == 0) { ProductViewModel po = new ProductViewModel(); po.Category = currentCategory; foreach (HtmlNode li in node.DescendantNodes().ToList()) { if (li.Name.ToLower() == "a") { foreach (HtmlAttribute at in li.Attributes) { if (at.Name.ToLower() == "href") { po.Href = at.Value; } } po.ShortDescription = ConvertWhitespacesToSingleSpaces(li.InnerText.Replace("�", "'")); } else if (li.Name.ToLower() == "span") { po.RecallDateString = li.InnerText; } else if (li.Name.ToLower() == "#text") { if (li.ParentNode.Name.ToLower() == "li" && li.InnerText.Contains("20")) { String date = ""; if (li.PreviousSibling != null && li.PreviousSibling.Name.ToLower() == "abbr") { date += li.PreviousSibling.InnerText + " "; } date += li.InnerText; po.RecallDateString = date; } } } tmpItems.Add(po); } } } foreach (ProductViewModel p in tmpItems.OrderByDescending(x => x._dateRecall)) { this.Items.Add(p); } this.IsDataLoaded = true; NotifyPropertyChanged("WebDataRetrived"); }
//Because current contains uncategorized items private void parse_RecallListCurrent(object sender, HtmlDocumentLoadCompleted e) { IList<ProductViewModel> tmpItems = new List<ProductViewModel>(); IList<HtmlNode> hnc = e.Document.DocumentNode.DescendantNodes().ToList(); String currentCategory = "Advisories, Warnings and Recalls"; foreach (HtmlNode node in hnc) { if (node.Name.ToLower() == "div") { if (node.Attributes.Count == 1 && node.Attributes[0].Name.ToLower() == "class" && node.Attributes[0].Value.ToLower() == "date") { foreach (HtmlNode child in node.DescendantNodes().ToList()) { if (child.Name.ToLower() == "li") { if (child.ParentNode.Name == "ul" && child.ParentNode.Attributes.Count == 0) { ProductViewModel po = new ProductViewModel(); po.Category = currentCategory; foreach (HtmlNode li in child.DescendantNodes().ToList()) { if (li.Name.ToLower() == "a") { foreach (HtmlAttribute at in li.Attributes) { if (at.Name.ToLower() == "href") { po.Href = at.Value; } } po.ShortDescription = ConvertWhitespacesToSingleSpaces(li.InnerText.Replace("�", "'")); } else if (li.Name.ToLower() == "span") { po.RecallDateString = li.InnerText; } else if (li.Name.ToLower() == "#text") { if (li.ParentNode.Name.ToLower() == "li" && li.InnerText.Contains("20")) { String date = ""; if (li.PreviousSibling != null && li.PreviousSibling.Name.ToLower() == "abbr") { date += li.PreviousSibling.InnerText + " "; } date += li.InnerText; po.RecallDateString = date; } } } //if (po.RecallDateString == "Jun 7, 2011") //{ // goto EndParse; //} tmpItems.Add(po); } } } } } } //EndParse: foreach (ProductViewModel p in tmpItems.OrderByDescending(x => x._dateRecall)) { this.Items.Add(p); } tmpItems.Clear(); this.IsDataLoaded = true; NotifyPropertyChanged("WebDataRetrived"); }
private void parse_ConsumerList(object sender, HtmlDocumentLoadCompleted e) { IList < ProductViewModel > tmpItems = new List<ProductViewModel>(); IList<HtmlNode> hnc = e.Document.DocumentNode.DescendantNodes().ToList(); String currentCategory = "Consumer Product Recalls"; foreach (HtmlNode node in hnc) { if (node.Name.ToLower() == "table") { foreach (HtmlAttribute att in node.Attributes) { if (att.Name.ToLower() == "summary" && att.Value == "Search results") { foreach (HtmlNode table in node.DescendantNodes().ToList()) { if (table.Name.ToLower() == "tr") { ProductViewModel po = new ProductViewModel(); po.Category = currentCategory; foreach (HtmlNode tr in table.DescendantNodes().ToList()) { if (tr.Name.ToLower() == "th") { break; } else if(tr.Name.ToLower() == "td") { String datetest = tr.InnerText; DateTime test; datetest = datetest.Replace(",", ""); datetest = datetest.Replace("Sept", "Sep"); datetest = datetest.Replace("y 008", "2008"); if (DateTime.TryParse(datetest, out test)) { po.RecallDateString = datetest; } else { po.ShortDescription = ConvertWhitespacesToSingleSpaces( this.getTitleFromURLNode(tr) ).Trim(); po.Href = Url_HealthCanadaConsumerPrefix + this.getURLFromNode(tr); break; } } } if (po.Href != null && po.Href != "") { tmpItems.Add(po); } } } } } } } foreach (ProductViewModel p in tmpItems.OrderByDescending(x => x._dateRecall)) { this.Items.Add(p); } tmpItems.Clear(); this.IsDataLoaded = true; NotifyPropertyChanged("WebDataRetrived"); }
private void load_FoodSafety(object sender, HtmlDocumentLoadCompleted e) { IList<ProductViewModel> tmpItems = new List<ProductViewModel>(); IList<HtmlNode> hnc = e.Document.DocumentNode.DescendantNodes().ToList(); String currentCategory = "Food Recalls and Allergy Alerts"; Boolean content_flag = false; foreach (HtmlNode node in hnc) { if (node.Name.ToLower() == "#comment" && node.InnerText.Contains("start - recall index links")) { content_flag = true; } else if(node.Name.ToLower() == "#comment" && node.InnerText.Contains("FOOTER BEGINS") ) { content_flag = false; } else if (content_flag && node.Name.ToLower() == "ul") { foreach (HtmlNode li in node.DescendantNodes().ToList()) { if (li.Name.ToLower() == "li") { ProductViewModel po = new ProductViewModel(); po.Category = currentCategory; foreach (HtmlNode ee in li.DescendantNodes().ToList()) { if (ee.Name.ToLower() == "strong") { po.RecallDateString = ee.InnerText; } else if (ee.Name.ToLower() == "a") { foreach (HtmlAttribute at in ee.Attributes) { if (at.Name.ToLower() == "href") { po.Href = "http://www.inspection.gc.ca"+ at.Value; } } po.ShortDescription = ConvertWhitespacesToSingleSpaces(ee.InnerText.Replace("�", "'").Replace("’", "'")); } } tmpItems.Add(po); } } } } foreach (ProductViewModel p in tmpItems.OrderByDescending(x => x._dateRecall)) { this.Items.Add(p); } tmpItems.Clear(); this.IsDataLoaded = true; NotifyPropertyChanged("WebDataRetrived"); }