HtmlAgilityPack multiple tbody in table

c# html-agility-pack

Question

In a table, there are many Tbodies, and I'm attempting to use HTMLagilitypack to separate them out. The code below should operate normally, but it doesn't. Currently, it ignores the second tbody and just publishes the first.

Code

var tableOffense =  doc.DocumentNode.SelectSingleNode("//table[@id='OFF']");
                var tbody = tableOffense.SelectNodes("tbody");
                foreach(var bodies in tbody)
                {
                    Console.WriteLine("id "+offender.offenderId +" "+ Utilities.RemoveHtmlCharacters(bodies.InnerText));
                }

HTML

<table id="OFF" class="centerTable" cols="2" style="margin-top:0; width:100%;" cellpadding="0" cellspacing="0">   
<tbody>
<!-- %%$SPLIT  -->
  <tr>         <th id="offenseCodeColHdr" scope="row" style="width:25%;" class="uline">Offense Code</th>         <td headers="offenseCodeColHdr" class="uline">288(a)</td>          </tr>       <tr>         <th id="descriptionColHdr" scope="row" style="width:25%;" class="uline">Description</th>         <td headers="descriptionColHdr" class="uline">LEWD OR LASCIVIOUS ACTS WITH A CHILD UNDER 14 YEARS OF AGE</td>             </tr>           <tr>         <th id="lastConvictionColHdr" scope="row" style="width:25%;" class="uline">Year of Last Conviction</th>         <td headers="lastConvictionColHdr" class="uline">&nbsp;</td>       </tr>       <tr>          <th id="lastReleaseColHdr" scope="row" style="width:25%;" class="uline">Year of Last Release</th>         <td headers="lastReleaseColHdr" class="uline">&nbsp;</td>       </tr>
  <tr><th colspan="2"><hr style="height:2px;background-color:#000;"></th></tr>         </tbody>
<!-- %%$SPLIT  -->
  <tbody><tr>         <th id="offenseCodeColHdr" scope="row" style="width:25%;" class="uline">Offense Code</th>         <td headers="offenseCodeColHdr" class="uline">261(a)(2)</td>            </tr>       <tr>         <th id="descriptionColHdr" scope="row" style="width:25%;" class="uline">Description</th>         <td headers="descriptionColHdr" class="uline">RAPE BY FORCE OR FEAR</td>          </tr>           <tr>         <th id="lastConvictionColHdr" scope="row" style="width:25%;" class="uline">Year of Last Conviction</th>         <td headers="lastConvictionColHdr" class="uline">&nbsp;</td>       </tr>       <tr>          <th id="lastReleaseColHdr" scope="row" style="width:25%;" class="uline">Year of Last Release</th>         <td headers="lastReleaseColHdr" class="uline">&nbsp;</td>       </tr>
  <tr><th colspan="2"><hr style="height:2px;background-color:#000;"></th></tr>         </tbody>
<!-- %%$SPLIT  -->
</table>

To verify that the second tbody existed at load, I printed simply the tableOffense node by itself.

Question Why doesn't the code display both tbodies, just the first one?

1
0
10/26/2015 6:00:15 PM

Accepted Answer

The reason your code only provides you one tbody is something I haven't worked out, however may I offer an other approach to pick all your<tbody> elements?

Personally, I would utilize XPAth to just pick every tbody element in a single operation without repeating the process.SelectNodes() :

var tbody = doc.DocumentNode.SelectNodes("//table[@id='OFF']//tbody");
foreach (var elem in tbody)
{
    //Dump only works in LinqPad
    elem.InnerText.Dump();
}

Edit:

The results produced by the following code (your code) are identical.

var tableOffense =  doc.DocumentNode.SelectSingleNode("//table[@id='OFF']");
var tbody = tableOffense.SelectNodes("//tbody");
1
10/26/2015 7:15:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow