Parsing HTML Reading Option Tag Content with HtmlAgillityPack

前端 未结 3 748
旧巷少年郎
旧巷少年郎 2020-12-06 22:52

I am trying to use HtmlAgilityPack to parse HTML, but am having problems.

Sample HTML Doc:


  

        
3条回答
  •  Happy的楠姐
    2020-12-06 23:10

    By default, the tag is treated by Html Agility Pack as "Empty", which means it does not need a closing . In this case, the closing tag is discarded. You can change this behavior using the HtmlNode.ElementFlags collection.

    Here is a code that should do what you want:

    HtmlDocument doc = new HtmlDocument();
    HtmlNode.ElementsFlags.Remove("option");
    doc.LoadHtml(yourHtml);
    
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//select[@id='onoffaci']//option"))
    {
        Console.WriteLine("Value=" + node.Attributes["value"].Value);
        Console.WriteLine("InnerText=" + node.InnerText);
        Console.WriteLine();
    }
    

提交回复
热议问题