Parse Html table data in WebBrowser

Posted on 2006-06-07
Last Modified: 2007-12-19
I'm trying to get data from table cells in a web page through the WebBrowser control in C# 2005. Below are some codes I used:

HtmlDocument doc = WebBrowser1.Document;
HtmlElementCollection tables = doc.GetElementsByTagName("table");
if (tables.Count > 0)
       System.Web.UI.HtmlControls.HtmlTable table = tables[0] as System.Web.UI.HtmlControls.HtmlTable;    //can't work

As known, tables[0] is treated as HtmlElement and C# doesn't allow me to convert this HtmlElement to an HtmlTable object. But I really hope I can use the easy functions from HtmlTable such as Rows and Cells to get data much faster and easier.

Is there a way for me to get data from the html table cells using the functions similar like HtmlTable?
Question by:ficstar
    LVL 96

    Assisted Solution

    by:Bob Learned
    1) You need to get the DomElement and cast that.

    2) Add a reference to Microsoft.mshtml

    3) Do it like this:

    if (tables.Count > 0)
           mshtml.HtmlTableElement table = (mshtml.HtmlTableElement)tables[0].DomElement;



    Author Comment

    Thanks for your help. I have to use mshtml.HTMLTable instead of the one you used since I cannot call mshtml.HtmlTableElement in my VS 2005:

    mshtml.HTMLTable table = (mshtml.HTMLTable)tables[i].DomElement

    This can work and I get the table data successfully.

    Can you tell me how to get the cell content?

    I tried:

    mshtml.HTMLTableCell cell = (mshtml.HTMLTableCell)table.cells.item(0, 0);
    string cellText = cell.innerText;

    The above codes didn't return the text in the first table cell but it's the whole table text body. How can I get the text in each table cell? Thanks.
    LVL 12

    Expert Comment

    'item' method only allows you to access an individual element from a collection if it has a 'name' attribute.
    You may use instead:

     foreach (mshtml.HTMLTableCellClass tblCell in table.cells)

    Author Comment

    This works great. However, I want to access the data for individual cells by using the row and column info. For example I want to know if cell(2, 5) contains the keyword "employee" or not. The foreach loop you gave me doesn't allow me to access individual cells in a way like that.
    LVL 12

    Accepted Solution


    HtmlElementCollection does not have an indexer, so its elements cannot be accessed by indexes. It only implements IEnumerator interface, it is not an IList. A workaround may be if you loop through rows collection, and for each row loop its cells collection, like:

    foreach (mshtml.HTMLTableRowClass row in table.rows)
          foreach (mshtml.HTMLTableCellClass cell in row.cells)
                Console.WriteLine(" Cell {0},{1} is: {2}",i,j,cell.innerHTML);

    Author Comment

    Great help. Thanks a lot.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now