Why is this HtmlAgilityPack operation invalid when there are, indeed, matching elements?

后端 未结 1 1360
春和景丽
春和景丽 2020-12-22 05:28

I get \"InvalidOperationException > Message=Sequence contains no matching element\" with the following code:

private void buttonLoadHTML_Click(object sender,         


        
相关标签:
1条回答
  • 2020-12-22 05:41

    It turns out to be pretty easy; this is not complete, but this, inspired by this answer, is enough to get started:

    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
    
    // There are various options, set as needed
    htmlDoc.OptionFixNestedTags = true;
    
    htmlDoc.Load(@"C:\Platypus\dplatypus.htm");
    
    if (htmlDoc.DocumentNode != null)
    {
        IEnumerable<HtmlAgilityPack.HtmlNode> textNodes = htmlDoc.DocumentNode.SelectNodes("//text()");
        foreach (HtmlNode node in textNodes)
        {
            if (!string.IsNullOrWhiteSpace(node.InnerText))
            {
                MessageBox.Show(node.InnerText);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题