Get a specific option in HtmlAgilityPack?

浪子不回头ぞ 提交于 2020-01-30 11:34:19

问题


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:


This should work:

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

You can read more about xpath here




回答2:


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.



来源:https://stackoverflow.com/questions/46590811/get-a-specific-option-in-htmlagilitypack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!