【问题标题】:HtmlAgilityPack multiple tbody in tableHtmlAgilityPack 表中的多个 tbody
【发布时间】:2015-10-26 17:37:26
【问题描述】:

一个表中有多个 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 而不是两者?

【问题讨论】:

  • 我在您的 Html 代码中只能看到一个 tbody 节点。这只是您有问题的 html 代码的一部分吗?
  • @Serv 修复了它,是的,它只是偶然的一部分

标签: c# html-agility-pack


【解决方案1】:

我还没有弄清楚为什么你的代码只给你一个 tbody,但我可以建议一个替代解决方案,选择你所有的 &lt;tbody&gt; 元素吗?

我个人会使用 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");

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 2012-05-07
    • 1970-01-01
    • 2017-10-15
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 2019-07-08
    相关资源
    最近更新 更多