我需要使用C#中的htmlagilitypack处理HTML。问题是我无法正确打印。 例1:
<tr class="even">
<td class="left">Names:</td><div class="field field-name-field-services field-type-reference field-label-hidden">
<div class="field-items">
<div class="field-item even">Abby</div>
<div class="field-item odd">Mary/Joe
</div><div class="field-item even">Cindy</div
><div class="field-item odd">Sally</div><div class="field-ite
m even">Zoey</div><div class="field-item odd">Donna</div><div class="field-item even">Kristie</div>
<div class="field-item odd">Mary</div><div
class="field-item even">Libby & Monroe</div><div class="field-item odd
">Nancy/Smith</div></div></div></td></tr>
我想要输出 Abby Mary / Joe Cindy Sally Donna Mary Libby&amp;梦露南希/史密斯。
示例2:
<tr class="even">
<td class="left">Names:</td><div class="field field-name-field-services field-type-reference field-label-hidden">
<div class="field-items">
<div class="field-item even">Cindy</div>
<div class="field-item odd">Sally</div><div class="field-item even">
Linda</div></div></div></div></td></tr>
输出是Cindy Sally Linda
这就是我的尝试。
HtmlDocument hdoc2 = new HtmlDocument();
hdoc2.LoadHtml(test);
var innernode1 = hdoc2.DocumentNode.SelectNodes("//tr//td");
Console.Writeline(innernode1[0].innertext);
我得到的对象实例未设置为对象错误的实例。它不应该打印出名字吗?
答案 0 :(得分:1)
尝试使用
hdoc2.DocumentNode.SelectNodes("//items/div[starts-with(@class, 'item')]/text()");
那应该选择你想要的所有文本节点。