c# - 内部包含内容的元素的 XPath?

标签 c# html xml xpath html-agility-pack

我正在使用 HtmlAgilityPack 并且遇到以下情况:

<table class='table-main odds '>   
   <tbody>
      <tr>..</tr>
      <tr>..</tr>
      <tr>..</tr>
      <tr></tr>
      <tr></tr>
  </tbody>
</table>          

因为您可以看到只有三个 tr 有内容,所以我希望在最终结果中应该只有前三个 tr 可用。实际上我的代码返回所有的 tr:

 HtmlNode oddsTable = doc.DocumentNode
          .SelectSingleNode("//table[starts-with(@class, 'table-main')]");
 HtmlNodeCollection rows = oddsTable.SelectNodes("tbody//tr");

如何使用 xpath 实现这一目标?

感谢您的帮助和解释。

最佳答案

取决于您所说的内容:

  • 任何内容://tr[node()]
  • 元素内容://tr[*]
  • 文字内容://tr[text()]
  • 非空白内容://tr[normalize-space()]

关于c# - 内部包含内容的元素的 XPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485687/

相关文章:

php - Delphi IdHTTP.get 和 PHP file_get_contents 显示相同 URL 的不同文本

c# - Ninject 特定于构造函数的代码顺序

c# - 如何修复 ASP.NET 中的 "System.IO.IOException: Unable to read data from the transport connection: The connection was closed"?

c# - 如何在C#或CS1503中使用通用变量

html - 不同部分的颜色在 webgrid 中显示不正确

html - 如何输出没有 "widows"的多列 html?

php - 数据库中的 HTML 代码

xml - 我怎样才能签署各种 XML 元素并签署他们的 parent ?

c# - 如何测试 XML 字符串在 .net 中的格式是否正确

c# - 如何消除 foreach 循环中的虚假/多余空格?