Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 to pass html text and get the contents of the first ordered list found in the html

private bool isOrderedList(HtmlNode node)
{
    if (node.NodeType == HtmlNodeType.Element)
    {
        if (node.Name.ToLower() == "ol")
            return true;
        else
            return false;
    }
    else
        return false;
}

public string GetOlList(string htmlText)
{
    string s="";
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(htmlText);
    HtmlNode nd = doc.DocumentNode;
    foreach (HtmlNode node in nd.ChildNodes)
    {
        if (isOrderedList(node))
        {
            s = node.WriteContentTo();
            break;
        }
        else if (node.HasChildNodes)
        {
            string sx= GetOlList(node.WriteTo());
            if (sx != "")
            {
                s = sx;
                break;
            }
        }
    }
    return s;
}
like image 444
Vamsi Avatar asked Dec 05 '10 12:12

Vamsi


2 Answers

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;
}
like image 120
Vamsi Avatar answered Oct 20 '22 01:10

Vamsi


How about:

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

(untested, from memory)

like image 25
Marc Gravell Avatar answered Oct 20 '22 01:10

Marc Gravell