HtmlDocument HtmlDocument HtmlDocument HtmlDocument Class

Definition

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

public ref class HtmlDocument sealed
public sealed class HtmlDocument
type HtmlDocument = class
Public NotInheritable Class HtmlDocument
Inheritance
HtmlDocumentHtmlDocumentHtmlDocumentHtmlDocument

Examples

Das folgende Codebeispiel verwendet die Daten aus der Northwind-Datenbank zum Erstellen einer HTML TABLE dynamisch mithilfe CreateElement.The following code example uses data from the Northwind database to create an HTML TABLE dynamically using CreateElement. Die AppendChild Methode auch zuerst verwendet wird, um Zellen hinzufügen (TD Elemente) auf Zeilen (TR Elemente), und dann zum Hinzufügen von Zeilen in die Tabelle und schließlich, in der Tabelle am Ende des aktuellen Dokuments angefügt werden soll.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. Im Codebeispiel wird vorausgesetzt, dass Ihre Anwendung eine WebBrowser Steuerelement mit dem Namen WebBrowser1.The code example requires that your application has a WebBrowser control named WebBrowser1. Der Code sollte aufgerufen werden, nachdem ein Dokument geladen wurde.The code should be called after a document has been loaded.

private void DisplayCustomersTable()
{
    DataSet customersSet = new DataSet();
    DataTable customersTable = null;
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Customers", "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
    sda.Fill(customersTable);
    customersTable = customersSet.Tables[0];

    if (webBrowser1.Document != null)
    {
        HtmlElement tableRow = null;
        HtmlElement headerElem = null;

        HtmlDocument doc = webBrowser1.Document;
        HtmlElement tableElem = doc.CreateElement("TABLE");
        doc.Body.AppendChild(tableElem);

        HtmlElement tableHeader = doc.CreateElement("THEAD");
        tableElem.AppendChild(tableHeader);
        tableRow = doc.CreateElement("TR");
        tableHeader.AppendChild(tableRow);

        foreach (DataColumn col in customersTable.Columns)
        {
            headerElem = doc.CreateElement("TH");
            headerElem.InnerText = col.ColumnName;
            tableRow.AppendChild(headerElem);
        }

        // Create table rows.
        HtmlElement tableBody = doc.CreateElement("TBODY");
        tableElem.AppendChild(tableBody);
        foreach (DataRow dr in customersTable.Rows)
        {
            tableRow = doc.CreateElement("TR");
            tableBody.AppendChild(tableRow);
            foreach (DataColumn col in customersTable.Columns)
            {
                Object dbCell = dr[col];
                HtmlElement tableCell = doc.CreateElement("TD");
                if (!(dbCell is DBNull))
                {
                    tableCell.InnerText = dbCell.ToString();
                }
                tableRow.AppendChild(tableCell);
            }
        }
    }
}
Private Sub DisplayCustomersTable()
    ' Initialize the database connection.
    Dim CustomerData As New DataSet()
    Dim CustomerTable As DataTable

    Try
        Dim DBConn As New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;")
        Dim DBQuery As New SqlDataAdapter("SELECT * FROM CUSTOMERS", DBConn)
        DBQuery.Fill(CustomerData)
    Catch dbEX As DataException

    End Try

    CustomerTable = CustomerData.Tables("Customers")

    If (Not (WebBrowser1.Document Is Nothing)) Then
        With WebBrowser1.Document
            Dim TableElem As HtmlElement = .CreateElement("TABLE")
            .Body.AppendChild(TableElem)

            Dim TableRow As HtmlElement

            ' Create the table header. 
            Dim TableHeader As HtmlElement = .CreateElement("THEAD")
            TableElem.AppendChild(TableHeader)
            TableRow = .CreateElement("TR")
            TableHeader.AppendChild(TableRow)

            Dim HeaderElem As HtmlElement
            For Each Col As DataColumn In CustomerTable.Columns
                HeaderElem = .CreateElement("TH")
                HeaderElem.InnerText = Col.ColumnName
                TableRow.AppendChild(HeaderElem)
            Next

            ' Create table rows.
            Dim TableBody As HtmlElement = .CreateElement("TBODY")
            TableElem.AppendChild(TableBody)
            For Each Row As DataRow In CustomerTable.Rows
                TableRow = .CreateElement("TR")
                TableBody.AppendChild(TableRow)
                For Each Col As DataColumn In CustomerTable.Columns
                    Dim Item As Object = Row(Col)
                    Dim TableCell As HtmlElement = .CreateElement("TD")
                    If Not (TypeOf (Item) Is DBNull) Then
                        TableCell.InnerText = CStr(Item)
                    End If
                    TableRow.AppendChild(TableCell)
                Next
            Next

        End With
    End If
End Sub

Remarks

HtmlDocument Stellt einen verwalteten Wrapper für Internet Explorer Document-Objekt, auch bekannt als das HTML Document Objekt Model (DOM) bereit.HtmlDocument provides a managed wrapper around Internet Explorer's document object, also known as the HTML Document Object Model (DOM). Sie erhalten eine Instanz der HtmlDocument über die Document Eigenschaft der WebBrowser Steuerelement.You obtain an instance of HtmlDocument through the Document property of the WebBrowser control.

HTML-Tags in einem HTML-Dokument können ineinander geschachtelt werden.HTML tags inside of an HTML document can be nested inside one another. HtmlDocument Daher stellt eine Dokumentstruktur, deren untergeordnete Instanzen Elemente von der HtmlElement Klasse.HtmlDocument thus represents a document tree, whose children are instances of the HtmlElement class. Das folgende Codebeispiel zeigt eine einfache HTML-Datei.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 diesem Beispiel HtmlDocument stellt dar, das gesamte Dokument innerhalb der HTML Tags.In this example, HtmlDocument represents the entire document inside the HTML tags. Die BODY, DIV, FORM und SPAN Tags werden durch einzelne dargestellt HtmlElement Objekte.The BODY, DIV, FORM and SPAN tags are represented by individual HtmlElement objects.

Es gibt mehrere Möglichkeiten, die Sie die Elemente in dieser Struktur zugreifen können.There are several ways you can access the elements in this tree. Verwenden der Body Eigenschaft, die Zugriff auf die BODY Tag und aller zugehörigen untergeordneten Elemente.Use the Body property to access the BODY tag and all of its children. Die ActiveElement Eigenschaft können Sie die HtmlElement für das Element auf einer HTML-Seite, die Benutzereingabefokus besitzt.The ActiveElement property gives you the HtmlElement for the element on an HTML page that has user input focus. Alle Elemente innerhalb einer HTML-Seite können es sich um einen Namen haben. die All Auflistung bietet Zugriff auf die einzelnen HtmlElement unter Verwendung des Namens als Index.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 Gibt eine HtmlElementCollection aller HtmlElement Objekten mit einem angegebenen HTML-Tag-Namen, z. B. DIV oder TABLE.GetElementsByTagName will return an HtmlElementCollection of all HtmlElement objects with a given HTML tag name, such as DIV or TABLE. GetElementById Gibt die einzelne HtmlElement für die eindeutige ID, die Sie angeben.GetElementById will return the single HtmlElement corresponding to the unique ID that you supply. GetElementFromPoint Gibt die HtmlElement , finden Sie auf dem Bildschirm auf den angegebenen Koordinaten Zeiger.GetElementFromPoint will return the HtmlElement that can be found on the screen at the supplied mouse pointer coordinates.

Sie können auch die Forms und Images Auflistung durchlaufen, Elemente, stellen Benutzer Eingabe Formen und Grafiken,.You can also use the Forms and Images collection to iterate through elements that represent user input forms and graphics, respectively.

HtmlDocument basiert auf den nicht verwalteten Schnittstellen, die von Internet Explorer DHTML-DOM-Implementierung: IHTMLDocument, IHTMLDocument2, IHTMLDocument3, und IHTMLDocument4.HtmlDocument is based on the unmanaged interfaces implemented by Internet Explorer's DHTML DOM: IHTMLDocument, IHTMLDocument2, IHTMLDocument3, and IHTMLDocument4. Nur die am häufigsten Eigenschaften verwendeten und Methoden für diese nicht verwalteten Schnittstellen verfügbar gemacht werden HtmlDocument.Only the most frequently used properties and methods on these unmanaged interfaces are exposed by HtmlDocument. Sie können Zugriff auf alle anderen Eigenschaften und Methoden, die direkt mit der DomDocument -Eigenschaft, die Sie in den gewünschten nicht verwalteten Schnittstellenzeiger umwandeln können.You can access all other properties and methods directly using the DomDocument property, which you can cast to the desired unmanaged interface pointer.

Ein HTML-Dokument darf Frames, die verschiedenen Fenstern werden in der die WebBrowser Steuerelement.An HTML document may contain frames, which are different windows inside of the WebBrowser control. Jeder Frame wird eine eigene HTML-Seite angezeigt.Each frame displays its own HTML page. Die Frames Auflistung steht über den Window Eigenschaft.The Frames collection is available through the Window property. Sie können auch die Window Eigenschaft, um die angezeigte Seite die Größe ändern, führen Sie das Dokument einen Bildlauf oder anzeigen, Warnungen und fordert den Benutzer.You may also use the Window property to resize the displayed page, scroll the document, or display alerts and prompts to the user.

HtmlDocument Stellt die am häufigsten aufgetretenen Ereignisse, die Sie erwarten, um beim Hosten von HTML-Seiten zu verarbeiten.HtmlDocument exposes the most common events you would expect to handle when hosting HTML pages. Für Ereignisse, die nicht direkt durch die Schnittstelle verfügbar gemacht werden, können Sie einen Handler für das Ereignis mithilfe hinzufügen AttachEventHandler.For events not exposed directly by the interface, you can add a handler for the event using AttachEventHandler.

HTML-Dateien enthalten möglicherweise SCRIPT Tags, die in einer von den Active Scripting-Sprachen, wie z. B. JScript oder VBScript geschriebenen Code zu kapseln.HTML files may contain SCRIPT tags that encapsulate code written in one of the Active Scripting languages, such as JScript or VBScript. Die InvokeScript Methode können Sie für die Ausführung von Eigenschaften und Methoden definiert, einem SCRIPT Tag.The InvokeScript method provides for execution of properties and methods defined in a SCRIPT tag.

Hinweis

Wenngleich die meisten Eigenschaften, Methoden und Ereignisse auf HtmlDocument haben die gleichen Namen beibehalten, da sie auf dem nicht verwalteten DOM haben, einige zonensignaturparametern für die Konsistenz mit der .NET Framework.NET Framework.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.NET Framework.

Properties

ActiveElement ActiveElement ActiveElement ActiveElement

Provides the HtmlElement which currently has user input focus. Provides the HtmlElement which currently has user input focus.

ActiveLinkColor ActiveLinkColor ActiveLinkColor ActiveLinkColor

Gets or sets the Color of a hyperlink when clicked by a user. Gets or sets the Color of a hyperlink when clicked by a user.

All All All All

Gets an instance of HtmlElementCollection, which stores all HtmlElement objects for the document. Gets an instance of HtmlElementCollection, which stores all HtmlElement objects for the document.

BackColor BackColor BackColor BackColor

Gets or sets the background color of the HTML document. Gets or sets the background color of the HTML document.

Body Body Body Body

Gets the HtmlElement for the BODY tag. Gets the HtmlElement for the BODY tag.

Cookie Cookie Cookie Cookie

Gets or sets the HTTP cookies associated with this document. Gets or sets the HTTP cookies associated with this document.

DefaultEncoding DefaultEncoding DefaultEncoding DefaultEncoding

Gets the encoding used by default for the current document. Gets the encoding used by default for the current document.

Domain Domain Domain Domain

Gets or sets the string describing the domain of this document for security purposes. Gets or sets the string describing the domain of this document for security purposes.

DomDocument DomDocument DomDocument DomDocument

Gets the unmanaged interface pointer for this HtmlDocument. Gets the unmanaged interface pointer for this HtmlDocument.

Encoding Encoding Encoding Encoding

Gets or sets the character encoding for this document. Gets or sets the character encoding for this document.

Focused Focused Focused Focused

Gets a value indicating whether the document has user input focus. Gets a value indicating whether the document has user input focus.

ForeColor ForeColor ForeColor ForeColor

Gets or sets the text color for the document. Gets or sets the text color for the document.

Forms Forms Forms Forms

Gets a collection of all of the <FORM> elements in the document. Gets a collection of all of the <FORM> elements in the document.

Images Images Images Images

Gets a collection of all image tags in the document. Gets a collection of all image tags in the document.

LinkColor LinkColor LinkColor LinkColor

Gets or sets the color of hyperlinks. Gets or sets the color of hyperlinks.

Links Links Links Links

Gets a list of all the hyperlinks within this HTML document. Gets a list of all the hyperlinks within this HTML document.

RightToLeft RightToLeft RightToLeft RightToLeft

Gets or sets the direction of text in the current document. Gets or sets the direction of text in the current document.

Title Title Title Title

Gets or sets the text value of the <TITLE> tag in the current HTML document. Gets or sets the text value of the <TITLE> tag in the current HTML document.

Url Url Url Url

Gets the URL describing the location of this document. Gets the URL describing the location of this document.

VisitedLinkColor VisitedLinkColor VisitedLinkColor VisitedLinkColor

Gets or sets the Color of links to HTML pages that the user has already visited. Gets or sets the Color of links to HTML pages that the user has already visited.

Window Window Window Window

Gets the HtmlWindow associated with this document. Gets the HtmlWindow associated with this document.

Methods

AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler) AttachEventHandler(String, EventHandler)

Adds an event handler for the named HTML DOM event. Adds an event handler for the named HTML DOM event.

CreateElement(String) CreateElement(String) CreateElement(String) CreateElement(String)

Creates a new HtmlElement of the specified HTML tag type. Creates a new HtmlElement of the specified HTML tag type.

DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler) DetachEventHandler(String, EventHandler)

Removes an event handler from a named event on the HTML DOM. Removes an event handler from a named event on the HTML DOM.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Tests the object for equality against the current object. Tests the object for equality against the current object.

ExecCommand(String, Boolean, Object) ExecCommand(String, Boolean, Object) ExecCommand(String, Boolean, Object) ExecCommand(String, Boolean, Object)

Executes the specified command against the document. Executes the specified command against the document.

Focus() Focus() Focus() Focus()

Sets user input focus on the current document. Sets user input focus on the current document.

GetElementById(String) GetElementById(String) GetElementById(String) GetElementById(String)

Retrieves a single HtmlElement using the element's ID attribute as a search key. Retrieves a single HtmlElement using the element's ID attribute as a search key.

GetElementFromPoint(Point) GetElementFromPoint(Point) GetElementFromPoint(Point) GetElementFromPoint(Point)

Retrieves the HTML element located at the specified client coordinates. Retrieves the HTML element located at the specified client coordinates.

GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String) GetElementsByTagName(String)

Retrieve a collection of elements with the specified HTML tag. Retrieve a collection of elements with the specified HTML tag.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Retrieves the hash code for this object. Retrieves the hash code for this object.

GetType() GetType() GetType() GetType()

Ruft den Type der aktuellen Instanz ab. Gets the Type of the current instance.

(Inherited from Object)
InvokeScript(String) InvokeScript(String) InvokeScript(String) InvokeScript(String)

Executes an Active Scripting function defined in an HTML page. Executes an Active Scripting function defined in an HTML page.

InvokeScript(String, Object[]) InvokeScript(String, Object[]) InvokeScript(String, Object[]) InvokeScript(String, Object[])

Executes an Active Scripting function defined in an HTML page. Executes an Active Scripting function defined in an HTML page.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object. Creates a shallow copy of the current Object.

(Inherited from Object)
OpenNew(Boolean) OpenNew(Boolean) OpenNew(Boolean) OpenNew(Boolean)

Gets a new HtmlDocument to use with the Write(String) method. Gets a new HtmlDocument to use with the Write(String) method.

ToString() ToString() ToString() ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt. Returns a string that represents the current object.

(Inherited from Object)
Write(String) Write(String) Write(String) Write(String)

Writes a new HTML page. Writes a new HTML page.

Events

Click Click Click Click

Occurs when the user clicks anywhere on the document. Occurs when the user clicks anywhere on the document.

ContextMenuShowing ContextMenuShowing ContextMenuShowing ContextMenuShowing

Occurs when the user requests to display the document's context menu. Occurs when the user requests to display the document's context menu.

Focusing Focusing Focusing Focusing

Occurs before focus is given to the document. Occurs before focus is given to the document.

LosingFocus LosingFocus LosingFocus LosingFocus

Occurs while focus is leaving a control. Occurs while focus is leaving a control.

MouseDown MouseDown MouseDown MouseDown

Occurs when the user clicks the left mouse button. Occurs when the user clicks the left mouse button.

MouseLeave MouseLeave MouseLeave MouseLeave

Occurs when the mouse is no longer hovering over the document. Occurs when the mouse is no longer hovering over the document.

MouseMove MouseMove MouseMove MouseMove

Occurs when the mouse is moved over the document. Occurs when the mouse is moved over the document.

MouseOver MouseOver MouseOver MouseOver

Occurs when the mouse is moved over the document. Occurs when the mouse is moved over the document.

MouseUp MouseUp MouseUp MouseUp

Occurs when the user releases the left mouse button. Occurs when the user releases the left mouse button.

Stop Stop Stop Stop

Occurs when navigation to another Web page is halted. Occurs when navigation to another Web page is halted.

Operators

Equality(HtmlDocument, HtmlDocument) Equality(HtmlDocument, HtmlDocument) Equality(HtmlDocument, HtmlDocument) Equality(HtmlDocument, HtmlDocument)

Returns a value that indicates whether the specified HtmlDocument instances represent the same value. Returns a value that indicates whether the specified HtmlDocument instances represent the same value.

Inequality(HtmlDocument, HtmlDocument) Inequality(HtmlDocument, HtmlDocument) Inequality(HtmlDocument, HtmlDocument) Inequality(HtmlDocument, HtmlDocument)

Returns a value that indicates whether the specified HtmlDocument instances do not represent the same value. Returns a value that indicates whether the specified HtmlDocument instances do not represent the same value.

Applies to

See Also