private List<LcboInventoryData2> ParseDocument(HtmlDocument doc, int productNumber) { //Stopwatch parseDocumentStopWatch = new Stopwatch(); //parseDocumentStopWatch.Start(); Dictionary<string, LcboInventoryData2> resultDictionary = new Dictionary<string, LcboInventoryData2>(); HtmlNodeCollection docDocumentNodeSelectNodes = doc.DocumentNode.SelectNodes(String.Format("//a[@id[starts-with(.,'{0}')]]", Resources.ItemAnchorIdPrefix)); if (docDocumentNodeSelectNodes != null) foreach (HtmlNode link in docDocumentNodeSelectNodes) { List<LcboInventoryData2> result = new List<LcboInventoryData2>(); HtmlAttribute linkId = link.Attributes["id"]; string idName = linkId.Value; string idNumber = idName.Substring(Resources.ItemAnchorIdPrefix.Length, idName.Length - Resources.ItemAnchorIdPrefix.Length - 1); LcboInventoryData2 currentInventoryData; if (!resultDictionary.ContainsKey(idNumber)) { currentInventoryData = new LcboInventoryData2(); resultDictionary.Add(idNumber, currentInventoryData); } else { currentInventoryData = resultDictionary[idNumber]; } string store = "?STORE="; string amp = "&"; int storeTextPosition = link.Attributes[Resources.Href].Value.IndexOf(store); int amppos = link.Attributes[Resources.Href].Value.IndexOf(amp); string storenumber = link.Attributes[Resources.Href].Value.Substring(storeTextPosition + store.Length, amppos - storeTextPosition - store.Length); currentInventoryData.StoreNumber = Convert.ToInt32(storenumber); currentInventoryData.ProductId = productNumber; switch (idName[idName.Length - 1]) { case '1': //City currentInventoryData.City = InventoryRetriever.ExtractCity(link); break; case '2': //Street address currentInventoryData.StreetAddress = InventoryRetriever.ExtractStreetAddress(link); break; case '6': //Inventory level currentInventoryData.NumberInStock = InventoryRetriever.ExtractInventoryLevel(link); break; default: break; } } //Trace.TraceInformation("Finished processing collection for product ID {1} after {0} milliseconds", parseDocumentStopWatch.ElapsedMilliseconds, productNumber); //parseDocumentStopWatch.Stop(); return new List<LcboInventoryData2>(resultDictionary.Values); }
private List<LcboInventoryData2> ParseString(string html, int productId) { List<LcboInventoryData2> result = new List<LcboInventoryData2>(); string target = "<a class=\"item-details-col6\""; using (StringReader reader = new StringReader(html)) { string line; while ((line = reader.ReadLine()) != null) { if (line.Contains(target)) { LcboInventoryData2 currentInventoryData = new LcboInventoryData2(); currentInventoryData.ProductId = productId; currentInventoryData.NumberInStock = InventoryRetriever.ExtractInventoryLevel(line); string store = "?STORE="; string amp = "&"; int storeTextPosition = line.IndexOf(store); int amppos = line.IndexOf(amp); string storenumber = line.Substring(storeTextPosition + store.Length, amppos - storeTextPosition - store.Length); currentInventoryData.StoreNumber = Convert.ToInt32(storenumber); result.Add(currentInventoryData); } } } return result; }
private static LcboInventoryData2 ExtractLcboInventoryData(string productNumber, HtmlNode link) { HtmlAttribute linkId = link.Attributes["id"]; LcboInventoryData2 currentInventoryData = new LcboInventoryData2(); if (linkId.Value.EndsWith("6")) { currentInventoryData.ProductId = Convert.ToInt32(productNumber); currentInventoryData.NumberInStock = InventoryRetriever.ExtractInventoryLevel(link); string store = "?STORE="; string amp = "&"; int storeTextPosition = link.Attributes["href"].Value.IndexOf(store); int amppos = link.Attributes["href"].Value.IndexOf(amp); string storenumber = link.Attributes["href"].Value.Substring(storeTextPosition + store.Length, amppos - storeTextPosition - store.Length); currentInventoryData.StoreNumber = Convert.ToInt32(storenumber); } return currentInventoryData; }