Search code examples
c#html-agility-pack

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");

Solution

  • This should work:

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

    You can read more about xpath here