Get a value from an html document

c# document html html-agility-pack nodes

Question

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

Accepted Answer

This will get the content regardless of your malformed HTML:

HtmlNodeCollection tables = hdoc.DocumentNode.SelectNodes("//table[1]");
HtmlNodeCollection cells = tables[0].SelectNodes("//tr/td");
var cellText = cell[0].InnerHtml;

You should fix your HTML though, close the <body> tag.


Popular Answer

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




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why