Html Agility Pack - <option> inner text

江枫思渺然 提交于 2019-12-04 04:46:37

问题


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 ?


回答1:


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();
}



回答2:


You need to get at the #text node. Try using this instead:

sb.Append("V")
  .Append(y)
  .Append(">")
  .Append(node.NextSibling.InnerText)
  .Append("/V")
  .Append(y)
  .Append(">")
  .AppendLine();



回答3:


Or you may change your XPath expression to "//select[@class='required-entry super-attribute-select']/option/following-sibling::text()"



来源:https://stackoverflow.com/questions/9771678/html-agility-pack-option-inner-text

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