Html Agility Pack - <option> inner text

c# html html-agility-pack option xpath

Question

I have problem with this html:

<select id="attribute1021" class="required-entry super-attribute-select" name="super_attribute[1021]">
<option value="">Choose an Option...</option>
<option value="281">001 Melaike</option>
<option value="280">002 Taronja</option>
<option value="289">003 Lill</option>
<option value="288">004 Chèn</option>
<option value="287">005 Addition</option>
<option value="286">006 Iskia</option>
<option value="285">007 Milele</option>
<option value="284">008 Cali</option>
<option value="283">009 Odessa</option>
<option value="282">010 Manaus</option>
<option value="303">011 Nartiss</option>
<option value="302">012 Curitiba</option>
<option value="301">013 Bogota</option>
<option value="300">014 Solèy</option>
<option value="299">015 Campinas</option>
<option value="298">016 Formosa</option>
<option value="297">017 Valencia</option>
<option value="296">018 Candu</option>
<option value="295">019 Medellín</option>
<option value="294">020 Incubo</option>
<option value="293">021 Belisama</option>
<option value="292">022 Amo</option>
<option value="291">023 Chimaira</option>
<option value="290">024 Matanza</option>
<option value="319">025 Baltimore</option>
</select>

With this code in C#

foreach (HtmlNode node in dok.DocumentNode.SelectNodes("//select[@class='required-entry super-attribute-select']/option"))
{
    sb.Append("V")
      .Append(y)
      .Append(">")
      .Append(node.InnerText)
      .Append("/V")
      .Append(y)
      .Append(">")
      .AppendLine();
}

But in inner text is only "Choose an Option..." .

Any idea how to fix it ?

Popular Answer

Html Agility Pack by default leaves option-Tags empty. To have it work you need remove the option-Tag from the list of elements that are left empty.

Just put the following somwhere before you load the Html.

HtmlNode.ElementsFlags.Remove("option");

var dok = new HtmlDocument();
dok.Load("option.htm");
var sb = new StringBuilder();
var y = "";
foreach (HtmlNode node in dok.DocumentNode.SelectNodes("//select[@class='required-entry super-attribute-select']/option"))
{
 sb.Append("V")
      .Append(y)
      .Append(">")
      .Append(node.InnerText)
      .Append("/V")
      .Append(y)
      .Append(">")
      .AppendLine();
}



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