HtmlAgilityPack表中的多個tbody

c# html-agility-pack

表中有多個Tbodies,我試圖通過使用HTMLagilitypack解析它們。通常,下面的代碼可以工作,但事實並非如此。現在它只打印第一個tbody並忽略第二個。

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>

我只打印了tableOffense節點,以確保第二個tbody在加載時存在,並且確實存在。

問題為什麼代碼只打印出第一個tbody而不是兩個?

一般承認的答案

我還沒弄清楚為什麼你的代碼只給你一個tbody,但我可以建議一個替代解決方案,選擇你所有的<tbody>元素嗎?

就個人而言,我會使用XPAth,只需一次選擇所有tbody元素,而無需額外的SelectNodes()

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

編輯:

以下代碼(您的代碼)也會產生相同的結果

var tableOffense =  doc.DocumentNode.SelectSingleNode("//table[@id='OFF']");
var tbody = tableOffense.SelectNodes("//tbody");


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因