html agility pack - Select specific node

c# html-agility-pack

Question

I need to select node which has a specific value:

<td class='formlabel'>Name:</td>

So I have something like this:

HtmlNode selectedNote = html.DocumentNode.SelectSingleNode("//td[@class='formlabel'][starts-with(., 'Name:')]");

This works. But the problem is that this is a selection with starts-with so for example if I have code like this:

<td class='formlabel'>Name: some text</td>

It will also select the node. I need something that will select node only if this exists

<td class='formlabel'>Name:</td>

So it will select node which has only innerText equals "Name:"

Hmm...Is this possible. Thank you very much.

Accepted Answer

I believe you want:

HtmlNode selectedNote = html.DocumentNode.SelectSingleNode("//td[@class='formlabel'][text()='Name:']");

Or

HtmlNode selectedNote = html.DocumentNode.SelectSingleNode("//td[@class='formlabel'][.='Name:']");



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