HtmlElement 使用 C# 获取具有特定 ID 的 table

HtmlElement get table with specific ID using C#

我有简单的代码可以读取 html 并从 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);                
        }

但是我想 id 阅读 table... 我该怎么做?

您可以使用 GetElementsById 通过 ID 获取 HTML 个元素。

var table =  testWebBrowser.Document.GetElementById("TableID");

你试过了吗GetElementById

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