ssdfsd
ssdfsd

Reputation: 1

Get a specific option in HtmlAgilityPack?

is possible get with HtmlAgilityPack a specific option? For example I've a select like this:

<select id="foo">
   <option value="0">1</option>
   <option value="1" selected="selected">2</option> 
</selected>

I need to get the option with selected. I know how to get the option with:

doc.DocumentNode.SelectNodes("//select[@id='foo']//option");

Upvotes: 0

Views: 1214

Answers (3)

mauriciosouza
mauriciosouza

Reputation: 31

if the html looks like this

<option value="1" selected>2</option> 

it should be like this

doc.DocumentNode.SelectSingleNode("//Select[@id='foo']//*[@selected='']");

Upvotes: 0

GammaSoul
GammaSoul

Reputation: 11

doc.DocumentNode.SelectSingleNode("//Select[@id='foo']//*[@selected='selected']");

This should work but its giving a wider birth to get it by attempting to get the first node it finds of any Tag type at any depth within the select Tag that has a selected Attribute of selected value.

Upvotes: 1

Gianlucca
Gianlucca

Reputation: 1354

This should work:

doc.DocumentNode.SelectNodes("//select[@id='foo']/option[@selected='selected']");

You can read more about xpath here

Upvotes: 1

Related Questions