コード例 #1
0
        private static void ProcessTd(HtmlNode td, ref Ai ai, ref int counter)
        { // we're in
            var child = td.FirstChild;

            if (child != null)
            {
                switch (counter)
                {
                case 0:
                    ai.Title = child.ParentNode.InnerText.Replace(" ", " ").Trim();
                    ai.Tags.Add(ai.Title);
                    Inform(ai.Title);
                    break;

                case 3:
                    ai.SalesUnit = child.InnerText.Substring(child.InnerText.IndexOf("(", StringComparison.Ordinal) + 1,
                                                             child.InnerText.IndexOf(")", StringComparison.Ordinal) - child.InnerText.IndexOf("(", StringComparison.Ordinal) - 1).Trim();
                    break;

                case 5:
                    ai.ProductType = child.InnerText.Replace(" ", " ");
                    break;

                case 6:
                    ai.Class = child.InnerText.Replace(" ", " ");
                    break;

                case 7:
                    ai.SalesAmount = child.InnerText.Replace(" ", " ");
                    break;

                case 8:
                    ai.LaunchDate = child.InnerText.Replace(" ", " ");
                    break;

                case 12:
                    ai.KeyManufacturerBrand = child.InnerText.Replace(" ", " ");
                    break;

                case 13:
                    ai.OtherManufacturer = child.InnerText.Replace(" ", " ");
                    break;

                case 11:
                    var img = child.ParentNode.SelectSingleNode("p/img");
                    if (img != null)
                    {
                        ai.StructureImageLink = img.GetAttributeValue("src", "");
                        try
                        {
                            ai.StructureImage = File.ReadAllBytes(ai.StructureImageLink);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        ai.StructureImageLink = child.ParentNode.InnerText.Replace("Structure", "").Replace(" ", "").Trim();
                    }


                    break;

                case 15:
                    ai.Timing = child.ParentNode.SelectNodes("p/span").Count == 2 ? child.ParentNode.InnerText.Replace("Timing:", "").Replace(" ", " ").Trim() : child.ParentNode.NextSibling.InnerText.Replace(" ", " ").Trim();
                    break;

                default:
                    //                     ai.MainCrops.Add(child.InnerText);
                    if (child.InnerText.Contains("Main Mixture Partners"))
                    {
                        ai.MainMixturePartners = $"{child.InnerText} ".Substring(23).Replace(" ", " ").Trim();
                    }
                    else if (child.InnerText.Contains("Recent History:"))
                    {
                        var paragraphs = child.ParentNode.SelectNodes("p");
                        foreach (var paragraph in paragraphs)
                        {
                            if (!paragraph.InnerText.StartsWith("Recent History"))
                            {
                                ai.RecentHistory += paragraph.InnerText.Replace(" ", " ") + "\n\n";
                            }
                        }
                    }
                    else if (child.ParentNode.InnerText.Trim().StartsWith("Rate "))
                    {
                        ai.RateAmount = child.InnerText.Substring(child.InnerText.IndexOf(":", StringComparison.Ordinal) + 1).Replace(" ", " ").Trim();
                        ai.RateUnit   = child.InnerText.Substring(child.InnerText.IndexOf("(", StringComparison.Ordinal) + 1,
                                                                  child.InnerText.IndexOf(")", StringComparison.Ordinal) - child.InnerText.IndexOf("(", StringComparison.Ordinal) - 1).Replace(" ", " ").Trim();
                    }
                    else if (child.InnerText.Trim() == "Main Crops")
                    {
                        HtmlNode current = child.ParentNode.ParentNode.NextSibling.SelectNodes("td").First();
                        for (var i = 0; i < 10; i++)
                        {
                            if (current.FirstChild.InnerText.Trim()
                                .StartsWith("Main Mixture") || current.FirstChild.InnerText.Trim()
                                .StartsWith("Recent History"))
                            {
                                continue;
                            }
                            var index = 0;
                            foreach (var n in current.ParentNode.ChildNodes)
                            {
                                if (index == 0)
                                {
                                    ai.MainCrops.AddRange(n.InnerText.Replace("&#xa0;", " ").Split(',').Select(s => s.Trim()));
                                }
                                else
                                {
                                    ai.MainPests.AddRange(n.InnerText.Replace("&#xa0;", " ").Split(',').Select(s => s.Trim()));
                                }
                                index++;
                            }

                            current = current.ParentNode.NextSibling.SelectNodes("td").First();
                        }
                    }

                    break;
                }
            }
        }