Get a specific option in HtmlAgilityPack?

c# html-agility-pack

Question

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");
1
0
10/5/2017 4:42:37 PM

Accepted Answer

This should work:

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

You can read more about xpath here

1
10/5/2017 4:44:11 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow