HtmlWindow.Document 屬性

定義

取得包含在視窗內的 HTML 文件。

public System.Windows.Forms.HtmlDocument Document { get; }
public System.Windows.Forms.HtmlDocument? Document { get; }

屬性值

如果已載入文件,則為 HtmlDocument 的有效執行個體。 如果此視窗包含 FRAMESET ,或目前未載入任何檔,則會傳回 null

範例

下列程式碼範例會檢查包含框架的頁面內的每份檔,並建立每個頁面所有傳出超連結的資料表,以供日後檢查。

private void GetLinksFromFrames()
{
    Hashtable linksTable = new Hashtable();
    string frameUrl;

    if (!(webBrowser1.Document == null))
    {
        HtmlWindow currentWindow = webBrowser1.Document.Window;
        if (currentWindow.Frames.Count > 0)
        {
            foreach (HtmlWindow frame in currentWindow.Frames)
            {
                frameUrl = frame.Url.ToString();
                Hashtable frameLinksHash = new Hashtable();

                linksTable.Add(frameUrl, frameLinksHash);
                foreach (HtmlElement hrefElement in frame.Document.Links)
                {
                    frameLinksHash.Add(hrefElement.GetAttribute("HREF"), "Url");
                }
            }
        }
        else
        {
            Hashtable docLinksHash = new Hashtable();
            linksTable.Add(webBrowser1.Document.Url.ToString(), docLinksHash);

            foreach (HtmlElement hrefElement in webBrowser1.Document.Links)
            {
                docLinksHash.Add(hrefElement.GetAttribute("HREF"), "Url");
            }
        }
    }
}

備註

一般而言,您將透過 Document 控制項的 WebBrowser 屬性來存取 Document 。 當您需要使用 集合存取 內 FRAME 的檔時, Frames 請使用這個屬性。

適用於

產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9