Search code examples
c#htmlelements

HtmlElement get table with specific ID using C#


I have simple code which can read html and write data from table:

        foreach (HtmlElement hrel in testWebBrowser.Document.GetElementsByTagName("table"))
        {
                HtmlElementCollection coll2 = hrel.GetElementsByTagName("tr");
                Console.WriteLine(coll2[0].InnerText);
                Console.WriteLine(coll2[1].InnerText);
                Console.WriteLine(coll2[2].InnerText);
                Console.WriteLine(coll2[3].InnerText);                
        }

But I want read table by id... How can i do this?


Solution

  • Have you tried GetElementById?

    HtmlElement table =  testWebBrowser.Document.GetElementById("TableID");
    if (table != null)
    {
        foreach (HtmlElement row in table.GetElementsByTagName("TR"))
        {
            // ...
        }
    }