Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

HtmlDocument Class

Provides top-level programmatic access to an HTML document hosted by the WebBrowser control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public sealed class HtmlDocument
public final class HtmlDocument
public final class HtmlDocument
Not applicable.

HtmlDocument provides a managed wrapper around Internet Explorer's document object, also known as the HTML Document Object Model (DOM). You obtain an instance of HtmlDocument through the Document property of the WebBrowser control.

HTML tags inside of an HTML document can be nested inside one another. HtmlDocument thus represents a document tree, whose children are instances of the HtmlElement class. The following code example shows a simple HTML file.

<HTML>
    <BODY>
        <DIV name="Span1">Simple HTML Form</DIV>
        <FORM>
            <SPAN name="TextLabel">Enter Your Name:</SPAN>
            <INPUT type="text" size="20" name="Text1">
        </FORM>
    </BODY>
</HTML>

In this example, HtmlDocument represents the entire document inside the HTML tags. The BODY, DIV, FORM and SPAN tags are represented by individual HtmlElement objects.

There are several ways you can access the elements in this tree. Use the Body property to access the BODY tag and all of its children. The ActiveElement property gives you the HtmlElement for the element on an HTML page that has user input focus. All elements within an HTML page can have a name; the All collection provides access to each HtmlElement using its name as an index. GetElementsByTagName will return an HtmlElementCollection of all HtmlElement objects with a given HTML tag name, such as DIV or TABLE. GetElementById will return the single HtmlElement corresponding to the unique ID that you supply. GetElementFromPoint will return the HtmlElement that can be found on the screen at the supplied mouse pointer coordinates.

You can also use the Forms and Images collection to iterate through elements that represent user input forms and graphics, respectively.

HtmlDocument is based on the unmanaged interfaces implemented by Internet Explorer's DHTML DOM: IHTMLDocument, IHTMLDocument2, IHTMLDocument3, and IHTMLDocument4. Only the most frequently used properties and methods on these unmanaged interfaces are exposed by HtmlDocument. You can access all other properties and methods directly using the DomDocument property, which you can cast to the desired unmanaged interface pointer.

An HTML document may contain frames, which are different windows inside of the WebBrowser control. Each frame displays its own HTML page. The Frames collection is available off of the Window property. You may also use the Window property to resize the displayed page, scroll the document, or display alerts and prompts to the user.

HtmlDocument exposes the most common events you would expect to handle when hosting HTML pages. For events not exposed directly by the interface, you can add a handler for the event using AttachEventHandler.

HTML files may contain SCRIPT tags that encapsulate code written in one of the Active Scripting languages, such as JScript or VBScript. The InvokeScript method provides for execution of properties and methods defined in a SCRIPT tag.

NoteNote:

While most of the properties, methods, and events on HtmlDocument have kept the same names as they have on the unmanaged DOM, some have been changed for consistency with the .NET Framework.

The following code example uses data from the Northwind database to create an HTML TABLE dynamically using CreateElement. The AppendChild method is also used, first to add cells (TD elements) to rows (TR elements), then to add rows to the table, and finally to append the table to the end of the current document. The code example requires that your application has a WebBrowser control called WebBrowser1.

System.Object
  System.Windows.Forms.HtmlDocument

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2014 Microsoft