HtmlAgilityPack HtmlNode how to get <select> and <option> tag

c# html-agility-pack

Question

I am using HtmlAgilityPack HtmlNode and i want to get

<select> and <option> and all other html tags.

Currently HtmlNode parse only html, table, div,... tags. How can i get option and select tag too.

My current code:

if (htmlContent != null)
{
    doc.LoadHtml(htmlContent);

    if (doc.DocumentNode.SelectNodes("//*") == null)
    {
        List<TagValuePair> tempList = new List<TagValuePair>();
        tempList.Add(new TagValuePair("Error!", htmlContent, -1));
        return tempList;
    }

    foreach (HtmlNode tag in doc.DocumentNode.SelectNodes("//*"))
    {
        try
        {
            if (!string.IsNullOrEmpty(tag.InnerHtml))
            {
                if (!tagAppearance.Keys.Contains(tag.Name))
                {
                    tagAppearance.Add(tag.Name, 1);
                }
                else
                    tagAppearance[tag.Name] = tagAppearance[tag.Name] + 1;

                tagsValues.Add(
                    new TagValuePair(tag.Name, tag.InnerHtml.Trim(),
                    tagAppearance[tag.Name])
                );
            }
        }
        catch
        {
            return null;
        }
    }
}

i try with

doc.DocumentNode.SelectNodes("//option"); 

and i get option tags but no others. How to get all + option and select

EDIT:

need to get all data from option from this html:

<select onchange="javascript:submitGlobalDiscountSvetisce(this.value);submitGlobalDiscount(this.value);" name="datumper">
                  <option value="18.6.2011|7">18.jun. 7 noči od 515,00&nbsp;EUR</option>
<option value="25.6.2011|7">25.jun. 7 noči od 515,00&nbsp;EUR</option>
<option value="2.7.2011|7">2.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="9.7.2011|7">9.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="16.7.2011|7">16.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="23.7.2011|7">23.jul. 7 noči od 515,00&nbsp;EUR</option>
<option value="30.7.2011|7">30.jul. 7 noči od 529,00&nbsp;EUR</option>
<option value="6.8.2011|7">6.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="13.8.2011|7">13.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="20.8.2011|7">20.avg. 7 noči od 529,00&nbsp;EUR</option>
<option value="3.9.2011|7">3.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="10.9.2011|7">10.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="17.9.2011|7">17.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="24.9.2011|7">24.sep. 7 noči od 487,00&nbsp;EUR</option>
<option value="1.10.2011|7">1.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="8.10.2011|7">8.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="15.10.2011|7">15.okt. 7 noči od 529,00&nbsp;EUR</option>
<option value="22.10.2011|7">22.okt. 7 noči od 567,00&nbsp;EUR</option>
<option value="29.10.2011|7">29.okt. 7 noči od 567,00&nbsp;EUR</option>

                </select>

Popular Answer

By default, OPTION tags are treated specifically (for historic reasons). See my answer on this thread here on SO: Parsing HTML Reading Option Tag Content with HtmlAgillityPack




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