c# - 从 html 文档中获取值

标签 c# html html-agility-pack document nodes

我需要一些帮助才能从 HTML 文档中获取值吗?

文档内容如下:

<html>
  <head>
    <style>body, table, input, select, textarea, button {   font: normal 1em Verdana, Sans-Serif; } body {  font-size: 0.8em; } a { color:#336600; } b { color:#003300; }.header {font-family: verdana; font-size: 15px; color:#003300; font-weight:bold;}.back {background-color:#DBF0DB;}.back2 {background-color:#009933;}            
    </style>
  </head>
  <body>
    <table border="0" cellpadding="3" cellspacing="1" width="100%">
      <tr>
        <td colspan="2" class="header">#827216</td>
      </tr>
    </table>
<body>
</html> 

我想检索 #827216 值。

这是我正在使用的代码,它不能正常工作:

hdoc.LoadHtml(FileContents);

var xID = hdoc.DocumentNode.SelectNodes("/html/body/table/tr/");

这里是错误:

Expression must evaluate to a node-set

最佳答案

您的 HTML 代码不是有效的 XML。 body 标签没有关闭。此外,您的 XPath 表达式应该是 /html/body/table/tr/td 才能到达 td 元素。此外,要获得一个元素,您应该使用 selectSingleNode

关于c# - 从 html 文档中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204174/

相关文章:

c# - C# 中的 Liang-Barsky?

c# - 如何在已知长度内精确匹配字母和数字的组合?

c# - C#中的动态变量?

javascript - 当用户给出日期并显示在该位置时计算确切日期时出现问题

c# - 如何使用Xpath获取嵌套div?

c# - 从c#插入毫秒到mysql

html - jQuery UI 可拖动使元素忽略 translate3d 变换

javascript - 使用 jQuery 更改目标属性

c# - 使用 agility pack C# 处理 cookie 和 header

c# - 使用 HTMLAgilityPack 将链接转换为 BBCODE?