HTMLAgilityPack parse in the InnerHTML

左心房为你撑大大i 提交于 2020-01-10 05:34:13

问题


<div>
<b>Token1</b>
Token2
<b>Token3</b>
</div>

I try to extract Token2 from the div

I manage to get Token1 and Token3 with :

HtmlNodeCollection headerFooter = doc.DocumentNode.SelectNodes("//div//b");

How can I extract directly Token2 with HTMLAgilityPack ?

One dirty option is to replace Token1 and Token2 by string.empty in doc.DocumentNode.SelectNodes("//div").InnerText, but I imagine it can been done in more clean way with HTMLAgilityPack...


回答1:


The text is in the text nodes; so you should be able to look at "//div/text()" and concatenate:

StringBuilder sb = new StringBuilder();
foreach (HtmlAgilityPack.HtmlTextNode node in
      doc.DocumentNode.SelectNodes("//div/text()"))
{
    sb.Append(node.Text.Trim());
}
string s = sb.ToString();


来源:https://stackoverflow.com/questions/1346144/htmlagilitypack-parse-in-the-innerhtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!