RamblerToning
RamblerToning

Reputation: 976

Looping through elements in a div in CsQuery

I'm trying to open an HTML file, loop through the divs that match a certain criteria, and then loop through the p tags that match a certain criteria within those divs.

CQ dom = CQ.CreateFromFile("page.html");
CQ document_divs = dom["div"];
document_divs.Each((i,document_div) =>
{
    string divid = document_div.Id;
    if (divid.Contains("page"))
    {
        CQ page_ptags = document_div["p"];
        page_ptags.Each((j, page_ptag) =>
        {
            lblOutput.Text = page_ptag.Id;
        });

    }

});

It is selecting the divs fine, but I'm not sure how to select the p tags within a div. I know there is something wrong with this line:

CQ page_ptags = document_div["p"];

But what should I change?

Upvotes: 1

Views: 917

Answers (2)

nazarkin659
nazarkin659

Reputation: 503

When you are looking throw a CQ object, each elements will be type of IDom. That's why you need or wrap it in CQ object, or use native Dom functions to work with.

Upvotes: 0

Paweł Bejger
Paweł Bejger

Reputation: 6366

Try this:

CQ page_ptags = document_div.Cq().Find("p");

Upvotes: 3

Related Questions