HtmlAgilityPack - nextSibling RRS feed

  • Question

  • HtmlWeb web = new HtmlWeb();
    HtmlDocument doc = web.Load(url);
    HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[@class='wisbb_scheduleTable']");
    foreach (HtmlNode thead in table.ChildNodes.Where(n => n.Name.Equals("thead")))
        HtmlNode tr = thead.ChildNodes.Where(n => n.Name.Equals("tr")).First();
        HtmlNode th = tr.ChildNodes.Where(n => n.Name.Equals("th")).First();
        HtmlNode tbody = thead.NextSibling;

    I am new to HtmlAgilityPack and I could use some help on the NextSibling function.  I thought that once I selected the first thead node that the next sibling would be the tbody node, but when I display the name of the node I get #text.  I have attached screen shots of the html document and the results I get from my script.  I collapsed the nodes of the html document to only show what I thought were all the sibling nodes so that it would be easier to read.

    Tuesday, October 24, 2017 4:13 PM


  • Probably there are some spaces, lines, or comments between <thead> and <tbody>. Try calling NextSibling again for each result to investigate this.

    Taking into consideration that <tbody> is not necessarily after <thead>, consider another way to find it.

    Wednesday, October 25, 2017 5:47 AM