HTMLAgilityPack仅迭代所有文本节点

Sha*_* Le 5 html-agility-pack

这是一个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>
Run Code Online (Sandbox Code Playgroud)

结果:

  1. 选择你的年龄:
  2. 0到10
  3. 20岁及以上
  4. 帮助/提示:
  5. 这是必填字段.
  6. 确保选择合适的年龄.
  7. 学到更多

Sim*_*ier 20

像这样的东西:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(yourHtmlFile);

    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//text()[normalize-space(.) != '']"))
    {
        Console.WriteLine(node.InnerText.Trim());
    }
Run Code Online (Sandbox Code Playgroud)

输出这个:

Select your Age:
0 to 10
20 and above
Help/Hints:
This is required field.
Make sure select the right age.
Learn More
Run Code Online (Sandbox Code Playgroud)