0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
André Silva
  • 1,149
  • 9
  • 30
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 09 '14 at 20:13

1 Answers1

2

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)
André Silva
  • 1,149
  • 9
  • 30
  • What did you have to change in order to fix it? – John Saunders Jan 09 '14 at 20:12
  • Sorry for taking so long to answer. Apparently `HTMLTableRow.cells` returns a `HTMLElementCollection` and each `HTMLElement` can be casted to a `HTMLTableCell`. It was just lack of attention in my part. – André Silva Jan 13 '14 at 09:07