Get value from html document

Can anyone help me get the value from an HTML document?

Here is the content of the document:

<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 want to get the value # 827216.

Here is the code I'm working with that doesn't work correctly:

hdoc.LoadHtml(FileContents);

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

And here is the error:

The expression must be evaluated in node-set

+4
source share
3 answers

This will allow you to get content regardless of your garbled HTML:

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

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

0
source

HTML- XML. body . XPath /html/body/table/tr/td, td. , , selectSingleNode

+2

Close the body tag and use SelectSingleNode

XmlDocument doc = new XmlDocument();
doc.Load("test.html");

var xID = doc.SelectSingleNode("/html/body/table/tr/td");
textBox1.Text = xID.InnerText;

I know this should be for xml, but it also works for html.

0
source

All Articles