none
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();
        Console.WriteLine(th.InnerHtml);
        HtmlNode tbody = thead.NextSibling;
        Console.WriteLine(tbody.Name);
    }
    

    https://www.dropbox.com/s/bvip9euhh9tjmdg/Screenshot%202017-10-24%2010.08.19.png?dl=0

    https://www.dropbox.com/s/trmnzskphrnk9d3/Screenshot%202017-10-24%2010.09.47.png?dl=0

    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

Answers

  • 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