Html Agility Pack - <option> inner text

北城余情 提交于 2019-12-01 23:32:07

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

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

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

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