HTMLAgilityPack仅迭代所有文本节点 [英] HTMLAgilityPack iterate all text nodes only
本文介绍了HTMLAgilityPack仅迭代所有文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个HTML代码段,我想要的只是获取文本节点并对其进行迭代.请让我知道.谢谢.
Here is a HTML snippet and all I want is to get only the text nodes and iterate them. Pls let me know. Thanks.
<div>
<div>
Select your Age:
<select>
<option>0 to 10</option>
<option>20 and above</option>
</select>
</div>
<div>
Help/Hints:
<ul>
<li>This is required field.
<li>Make sure select the right age.
</ul>
<a href="#">Learn More</a>
</div>
</div>
结果:
- 选择您的年龄:
- 0到10
- 20岁及以上
- 帮助/提示:
- 这是必填字段.
- 确保选择正确的年龄.
- 了解更多信息
推荐答案
类似以下内容:
HtmlDocument doc = new HtmlDocument();
doc.Load(yourHtmlFile);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']"))
{
Console.WriteLine(node.InnerText.Trim());
}
将输出以下内容:
Select your Age:
0 to 10
20 and above
Help/Hints:
This is required field.
Make sure select the right age.
Learn More
这篇关于HTMLAgilityPack仅迭代所有文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文