C#为什么方法HtmlDocument.GetElementById匹配标签的属性名称?

lau*_*zzc 9 c# dom getelementbyid

HtmlDocument.GetElementById("$id") 
Run Code Online (Sandbox Code Playgroud)

我想使用此方法来获取元素$id,但它匹配具有meta与其具有相同值的属性的标记$id.

HtmlDocument是这样的:

<html>
    <head>
        <meta name="description" content="">
    </head>
    <body>
        <div id="description"></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我试图获取divid为"description"的标签:

HtmlElement elem = doc.GetElementById("description");
Run Code Online (Sandbox Code Playgroud)

但我得到了meta而不是div.为什么meta标签匹配?

小智 2

为什么下面是微软官方的参考: getElementById 方法:返回对第一个具有指定 IDNAME属性值的对象的引用。

解决方案:您应该避免在正文中使用名称 attrib,以便如果您使用 sharique ansari 提到的 myHtmlDocument.Body.All[id] 公式,则可以通过 id 引用标签。

干杯