0

I have the following table and I want to extract the values Value_1 and Value_2 by matching Description_Line_1 and Description_Line_2.

<table id="tableId" class="tableClass">
<tr>
<th class="thClass1">
Description_Line_1
</th>
<td class="tdClass1">
Value_1
</td>
</tr>
<tr>
<th class="thClass2">
Description_Line_2
</th>
<td class="tdClass2">
Value_2
</td>
</tr>
</table>

The code I came up with is below, but it raises a null reference exception.

string filename = "TableSample.htm";
HtmlDocument doc = new HtmlDocument();
doc.Load(filename);
var tableNode = doc.DocumentNode.SelectSingleNode("//table[@id='tableId' and class='tableClass']//tr");
var value1 = tableNode.SelectSingleNode(".//th[contains(text(),'Description_Line_1')]/td/text()").InnerText;
Umair Mubeen
  • 823
  • 4
  • 22
Nick_F
  • 1,103
  • 2
  • 13
  • 30
  • Does this answer your question? [How to use HTML Agility pack](https://stackoverflow.com/questions/846994/how-to-use-html-agility-pack) – Umair Mubeen Mar 30 '21 at 10:36
  • Thanks for the link. It helps, but not as much as a (small?) correction in my code :) – Nick_F Mar 30 '21 at 10:38
  • I figured out the solution: var value1 = doc.DocumentNode.SelectSingleNode(".//tr[./th[contains(text(),'Description_Line_1')]]//td/text()").InnerText; – Nick_F Mar 30 '21 at 11:44
  • maybe this link will help you https://stackoverflow.com/questions/655603/html-agility-pack-parsing-tables – zahra zamani Apr 07 '21 at 11:57

0 Answers0