C# - HTML Agility Pack - Node Collection - Loop table rows, selecting nth-child(2)

c# html-agility-pack

Question

I'm wokring with htmlAgilityPack, and am grabbing a table from a website.

How can I modify this to return values for each row, every second column only.

public static void SearchAnimal(string param)
        {
            string prm = param;
            string url = "http://xxx/xxx.action?name=";
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url+prm);
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //StreamReader stream = new StreamReader(response.GetResponseStream());
            //string final_response = stream.ReadToEnd();
            var webGet = new HtmlWeb();
            var doc = webGet.Load(url + prm);

            HtmlNodeCollection tr = doc.DocumentNode.SelectNodes("//table[@id='animal']//tbody//tr//td");

                  for(int i = 0; i <= tr.Count; ++i){
                    var link = tr
                       .Descendants("a")
                       .First(x => x.Attributes["href"] != null);
                    string hrefValue = link.Attributes["href"].Value;
                    string name = link.InnerHtml;
                    Match match = Regex.Match(hrefValue, @"(\d+)$");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("Result " + tr + ":");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Animal Name: " + name);
                    Console.WriteLine("Animal Key: " + match.Value);
                    Console.WriteLine("-------------------------");
                    Console.WriteLine("");

                       }



        }

Accepted Answer

You can use XPath position filter to get only 2nd <td> child from each <tr> :

//table[@id='animal']//tbody//tr/td[2]

It is actually equals to CSS :nth-of-type() selector, and shows the same output as :nth-child() only if all children are of the same type (means all children are <td> in this case).



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why