Il y a plusieurs Tbodies dans un tableau et j'essaie de les analyser en utilisant HTMLagilitypack. Normalement, le code ci-dessous fonctionnerait, mais pas. Pour le moment, il n’imprime que le premier tbody et ignore le second.
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"> </td> </tr> <tr> <th id="lastReleaseColHdr" scope="row" style="width:25%;" class="uline">Year of Last Release</th> <td headers="lastReleaseColHdr" class="uline"> </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"> </td> </tr> <tr> <th id="lastReleaseColHdr" scope="row" style="width:25%;" class="uline">Year of Last Release</th> <td headers="lastReleaseColHdr" class="uline"> </td> </tr>
<tr><th colspan="2"><hr style="height:2px;background-color:#000;"></th></tr> </tbody>
<!-- %%$SPLIT -->
</table>
J'ai imprimé juste le nœud tableOffense par lui-même pour m'assurer que le 2nd tbody existe à la charge et qu'il existe.
Question Pourquoi le code n'imprime-t-il que le premier corps et non les deux?
Je n'ai pas compris pourquoi votre code ne vous donne qu'un tbody, mais puis-je suggérer une solution alternative pour sélectionner tous vos éléments <tbody>
?
Personnellement, je me servirais de XPAth et je sélectionnerais tous les éléments tbody en une fois, sans SelectNodes()
supplémentaire:
var tbody = doc.DocumentNode.SelectNodes("//table[@id='OFF']//tbody");
foreach (var elem in tbody)
{
//Dump only works in LinqPad
elem.InnerText.Dump();
}
Modifier:
Le code suivant (votre code) donne également les mêmes résultats
var tableOffense = doc.DocumentNode.SelectSingleNode("//table[@id='OFF']");
var tbody = tableOffense.SelectNodes("//tbody");