这是一个HTML片段,我想要的只是获取文本节点并迭代它们.请告诉我.谢谢.
<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