Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a value from an html document

May I have some help to get a value from an HTML document?

Here is the document contents:

<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> 

I am wanting to retrieve the #827216 value.

Here is the code I am working with, that does not work correctly:

hdoc.LoadHtml(FileContents);

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

And here is the error:

Expression must evaluate to a node-set

like image 710
user2985419 Avatar asked Nov 02 '22 10:11

user2985419


1 Answers

Your HTML code is not valid XML. The body tag is not closed. Also your XPath expression should be /html/body/table/tr/td to get to the td element. Also, to get one element you should use selectSingleNode

like image 199
Tommy Ovesen Avatar answered Nov 09 '22 22:11

Tommy Ovesen