C# 如何访问<;头>;在WinForms中使用webbrower控件的HTML文档的元素?

C# 如何访问<;头>;在WinForms中使用webbrower控件的HTML文档的元素?,c#,.net,html,winforms,webbrowser-control,C#,.net,Html,Winforms,Webbrowser Control,我正在使用WinForms C#3.5 WebBrowser 我正在尝试访问以下HTML文档中的head元素: this.webBrowserTest.DocumentText = @”<html> <head> <title>Test JavaScript WinForms</title> <meta name="description" content="Test WinForms" />

我正在使用WinForms C#3.5 WebBrowser

我正在尝试访问以下HTML文档中的head元素:

this.webBrowserTest.DocumentText = @”<html>
    <head>
        <title>Test JavaScript WinForms</title> 
        <meta name="description" content="Test WinForms" />        
    </head>
    <body></body>
</html>”

HtmlElementCollection headCollection = webBrowserTest.Document.GetElementsByTagName("HEAD");
HtmlElement head = headCollection[0]
this.webBrowserTest.DocumentText=@”
测试JavaScript WinForms
”
HtmlElementCollection headCollection=webBrowserTest.Document.GetElementsByTagName(“HEAD”);
HtmlElement头=头集合[0]
headCollection[0]
被传递为
null

有人知道怎么了吗

谢谢你试过了吗

webBrowserTest.Document.All["HEAD"]; // or head
你试过了吗

webBrowserTest.Document.All["HEAD"]; // or head

如果要为head元素提供id=“headid”属性,则可以使用webBrowserTest.Document.All[“headid”]

如果要为head元素提供id=“headid”属性,则可以使用webBrowserTest.Document.All[“headid”]

获取
头的代码看起来不错。在访问文档之前,请确保文档已完全加载。您可以通过执行以下操作来完成此任务:

// Add a handler for load complete.
webBrowserTest.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentLoadCompleted);

// Wait until load completes.
while (webBrowserTest.ReadyState != WebBrowserReadyState.Complete)
{
    System.Windows.Forms.Application.DoEvents();
}

// On load complete, do stuff.
private void DocumentLoadCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElement head = browser.Document.GetElementsByTagName("head")[0];
    // do stuff...
}

获取
头标记的代码看起来不错。在访问文档之前,请确保文档已完全加载。您可以通过执行以下操作来完成此任务:

// Add a handler for load complete.
webBrowserTest.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentLoadCompleted);

// Wait until load completes.
while (webBrowserTest.ReadyState != WebBrowserReadyState.Complete)
{
    System.Windows.Forms.Application.DoEvents();
}

// On load complete, do stuff.
private void DocumentLoadCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    HtmlElement head = browser.Document.GetElementsByTagName("head")[0];
    // do stuff...
}

谢谢你,彼得,但恐怕这也不行。我看到“webBrowserTest.Document.All”属性的计数为零。还有其他建议吗?真奇怪。以下是我的工作:
private void Form1_Load(object sender,EventArgs e){webBrowser1.DocumentText=@“Test JavaScript WinForms”}private void按钮1_单击(object sender,EventArgs e){textBox1.Text=webBrowser1.Document.All[“headid”].OuterHtml;}
谢谢Petr,但我担心这也不起作用。我看到“webBrowserTest.Document.All”属性的计数为零。还有其他建议吗。这很奇怪。以下方法对我有效:
私有void Form1_Load(对象发送方,事件参数e){webBrowser1.DocumentText=@”测试JavaScript WinForms”;}私有无效按钮1_单击(对象发送者,事件参数e){textBox1.Text=webBrowser1.Document.All[“headid”].OuterHtml;}
谢谢abatishchev。请参阅我对上述Petr的评论-“webBrowserTest.Document.All”“的计数为零。@hungoverbunny:请改用
WebBrowser.Document.Write()
方法。谢谢abatishchev。请参阅我对以上Petr的评论——“webBrowserTest.Document.All”的计数为零。@hungoverbunny:改用
WebBrowser.Document.Write()
方法。您是否等到DocumentCompleted?设置DocumentText会导致转储当前文档,并且在异步导航完成对新文档的解析之前,文档将不可用。是否要等到DocumentCompleted?设置DocumentText会导致转储当前文档,并且在异步导航完成对新文档的解析之前,文档将不可用。