How to get the contents of a HTML element using HtmlAgilityPack in C#?

后端 未结 2 843
轻奢々
轻奢々 2021-01-18 18:58

I want to get the contents of an ordered list from a HTML page using HTMLAgilityPack in C#, i have tried the following code but, this is not working can anyone help, i want

相关标签:
2条回答
  • 2021-01-18 19:02

    How about:

    var el = (HtmlElement)doc.DocumentNode
        .SelectSingleNode("//ol");
    if(el!=null)
    {
        string s = el.OuterHtml;
    }
    

    (untested, from memory)

    0 讨论(0)
  • 2021-01-18 19:08

    The following code worked for me

    public static string GetComments(string html)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        string s = "";
        foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//ol"))
        {
            s += node.OuterHtml;
        }
    
        return s;
    }
    
    0 讨论(0)
提交回复
热议问题