Search code examples
c#wpfvisual-studio-2010webbrowser-controlmshtml

Retrieve information from Table cell


I'm reading the page loaded in the WebBrowser control from my WPF application.

There is a table of data in this page that I need to capture it and I'm using MSHTML for it.

I can retrieve the table, I can retrieve the rows from the table, I just can't retrieve the cells from the rows. I always get a NullReferenceException.

This is what I have now :

foreach (var a in ((wbSocial.Document as HTMLDocument).getElementById("j_idt29:gridDadosTrabalhador").children as IHTMLElementCollection))
    foreach (var b in (a as HTMLTableSection).rows)
        if (((b as HTMLTableRow).cells as HTMLTableCell) == null || ((b as HTMLTableRow).cells as HTMLTableCell).nodeName.ToUpper() == "TH")
            continue;

When I debug the HTMLTableRow I see that there are 7 elements inside the HTMLTableRow, but if I cast to a HTMLTableCell it gives me the NullReferenceException.


Solution

  • Ok, I managed to solve it myself. I really thought it was something really far from my capability. After making it work, I really find myself stupid.

    This is what I did :

     foreach (var a in ((wbSocial.Document as HTMLDocument).getElementById("j_idt29:gridDadosTrabalhador").children as IHTMLElementCollection))
                        foreach (var b in (a as HTMLTableSection).rows)
                        {
                            if ((b as HTMLTableRow).rowIndex == 0) { continue; }
                            else
                            {
                                foreach (var c in (b as HTMLTableRow).cells)