我需要一些帮助才能从 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/