HtmlElement HtmlElement HtmlElement HtmlElement Class

Definition

Represents an HTML element inside of a Web page.

public ref class HtmlElement sealed
public sealed class HtmlElement
type HtmlElement = class
Public NotInheritable Class HtmlElement
Inheritance
HtmlElementHtmlElementHtmlElementHtmlElement

Examples

The following code example shows how to examine an arbitrary HTML document and derive a string describing the HTML elements, with indentation and level numbers used to indicate how deeply nested the elements are in the document. This code example requires that your application hosts a WebBrowser control named WebBrowser1.

private void PrintDomBegin()
{
	if (webBrowser1.Document != null)
	{
		HtmlElementCollection elemColl = null;
		HtmlDocument doc = webBrowser1.Document;
		if (doc != null)
		{
			elemColl = doc.GetElementsByTagName("HTML");
			String str = PrintDom(elemColl, new System.Text.StringBuilder(), 0);
			webBrowser1.DocumentText = str;
		}
	}
}

private string PrintDom(HtmlElementCollection elemColl, System.Text.StringBuilder returnStr, Int32 depth)
{
	System.Text.StringBuilder str = new System.Text.StringBuilder();

	foreach (HtmlElement elem in elemColl)
	{
		string elemName;

		elemName = elem.GetAttribute("ID");
		if (elemName == null || elemName.Length == 0)
		{
			elemName = elem.GetAttribute("name");
			if (elemName == null || elemName.Length == 0)
			{
				elemName = "<no name>";
			}
		}

		str.Append(' ', depth * 4);
		str.Append(elemName + ": " + elem.TagName + "(Level " + depth + ")");
		returnStr.AppendLine(str.ToString());

		if (elem.CanHaveChildren)
		{
			PrintDom(elem.Children, returnStr, depth + 1);
		}

		str.Remove(0, str.Length);
	}

	return(returnStr.ToString());
}
Private Sub PrintDomBegin()
    If (WebBrowser1.Document IsNot Nothing) Then
        Dim ElemColl As HtmlElementCollection

        Dim Doc As HtmlDocument = WebBrowser1.Document
        If (Not (Doc Is Nothing)) Then
            ElemColl = Doc.GetElementsByTagName("HTML")
            Dim Str As String = PrintDom(ElemColl, New System.Text.StringBuilder(), 0)

            WebBrowser1.DocumentText = Str
        End If
    End If
End Sub

Private Function PrintDom(ByVal ElemColl As HtmlElementCollection, ByRef ReturnStr As System.Text.StringBuilder, ByVal Depth As Integer) As String
    Dim Str As New System.Text.StringBuilder()

    For Each Elem As HtmlElement In ElemColl
        Dim ElemName As String

        ElemName = Elem.GetAttribute("ID")
        If (ElemName Is Nothing Or ElemName.Length = 0) Then
            ElemName = Elem.GetAttribute("name")
            If (ElemName Is Nothing Or ElemName.Length = 0) Then
                ElemName = "<no name>"
            End If
        End If

        Str.Append(CChar(" "), Depth * 4)
        Str.Append(ElemName & ": " & Elem.TagName & "(Level " & Depth & ")")
        ReturnStr.AppendLine(Str.ToString())

        If (Elem.CanHaveChildren) Then
            PrintDom(Elem.Children, ReturnStr, Depth + 1)
        End If

        Str.Remove(0, Str.Length)
    Next

    PrintDom = ReturnStr.ToString()
End Function

Remarks

HtmlElement represents any possible type of element in an HTML document, such as BODY, TABLE, and FORM, among others. The class exposes the most common properties you can expect to find on all elements.

Most elements can have child elements: other HTML elements that are placed underneath them. Use the CanHaveChildren property to test whether a given element has children, and the Children collection to iterate through them. The Parent property returns the HtmlElement in which the current element is nested.

You often need access to attributes, properties, and methods on the underlying element that are not directly exposed by HtmlElement, such as the SRC attribute on an IMG element or the Submit method on a FORM. The GetAttribute and SetAttribute methods enable you to retrieve and alter any attribute or property on a specific element, while InvokeMember provides access to any methods not exposed in the managed Document Object Model (DOM). If your application has unmanaged code permission, you can also access unexposed properties and methods with the DomElement attribute.

Use the TagName property to test whether an element is of a specific type.

Any HTML document can be modified at run time. You can create new HtmlElement objects with the CreateElement method of HtmlDocument, and add them to another element using the AppendChild or InsertAdjacentElement methods. You can also create the elements as HTML tags and assign them to an existing element's InnerHtml property.

Properties

All All All All

Gets an HtmlElementCollection of all elements underneath the current element.

CanHaveChildren CanHaveChildren CanHaveChildren CanHaveChildren

Gets a value indicating whether this element can have child elements.

Children Children Children Children

Gets an HtmlElementCollection of all children of the current element.

ClientRectangle ClientRectangle ClientRectangle ClientRectangle

Gets the bounds of the client area of the element in the HTML document.

Document Document Document Document

Gets the HtmlDocument to which this element belongs.

DomElement DomElement DomElement DomElement

Gets an unmanaged interface pointer for this element.

Enabled Enabled Enabled Enabled

Gets or sets whether the user can input data into this element.

FirstChild FirstChild FirstChild FirstChild

Gets the next element below this element in the document tree.

Id Id Id Id

Gets or sets a label by which to identify the element.

InnerHtml InnerHtml InnerHtml InnerHtml

Gets or sets the HTML markup underneath this element.

InnerText InnerText InnerText InnerText

Gets or sets the text assigned to the element.

Name Name Name Name

Gets or sets the name of the element.

NextSibling NextSibling NextSibling NextSibling

Gets the next element at the same level as this element in the document tree.

OffsetParent OffsetParent OffsetParent OffsetParent

Gets the element from which OffsetRectangle is calculated.

OffsetRectangle OffsetRectangle OffsetRectangle OffsetRectangle

Gets the location of an element relative to its parent.

OuterHtml OuterHtml OuterHtml OuterHtml

Gets or sets the current element's HTML code.

OuterText OuterText OuterText OuterText

Gets or sets the current element's text.

Parent Parent Parent Parent

Gets the current element's parent element.

ScrollLeft ScrollLeft ScrollLeft ScrollLeft

Gets or sets the distance between the edge of the element and the left edge of its content.

ScrollRectangle ScrollRectangle ScrollRectangle ScrollRectangle

Gets the dimensions of an element's scrollable region.

ScrollTop ScrollTop ScrollTop ScrollTop

Gets or sets the distance between the edge of the element and the top edge of its content.

Style Style Style Style

Gets or sets a semicolon-delimited list of styles for the current element.

TabIndex TabIndex TabIndex TabIndex

Gets or sets the location of this element in the tab order.

TagName TagName TagName TagName

Gets the name of the HTML tag.

Methods

AppendChild(HtmlElement) AppendChild(HtmlElement) AppendChild(HtmlElement) AppendChild(HtmlElement)

Adds an element to another element's subtree.

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

Adds an event handler for a named event on the HTML Document Object Model (DOM).

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

Removes an event handler from a named event on the HTML Document Object Model (DOM).

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

Tests if the supplied object is equal to the current element.

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

Puts user input focus on the current element.

GetAttribute(String) GetAttribute(String) GetAttribute(String) GetAttribute(String)

Retrieves the value of the named attribute on the element.

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

Retrieves a collection of elements represented in HTML by the specified HTML tag.

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

Serves as a hash function for a particular type.

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

Gets the Type of the current instance.

(Inherited from Object)
InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement) InsertAdjacentElement(HtmlElementInsertionOrientation, HtmlElement)

Insert a new element into the Document Object Model (DOM).

InvokeMember(String) InvokeMember(String) InvokeMember(String) InvokeMember(String)

Executes an unexposed method on the underlying DOM element of this element.

InvokeMember(String, Object[]) InvokeMember(String, Object[]) InvokeMember(String, Object[]) InvokeMember(String, Object[])

Executes a function defined in the current HTML page by a scripting language.

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

Creates a shallow copy of the current Object.

(Inherited from Object)
RaiseEvent(String) RaiseEvent(String) RaiseEvent(String) RaiseEvent(String)

Causes the named event to call all registered event handlers.

RemoveFocus() RemoveFocus() RemoveFocus() RemoveFocus()

Removes focus from the current element, if that element has focus.

ScrollIntoView(Boolean) ScrollIntoView(Boolean) ScrollIntoView(Boolean) ScrollIntoView(Boolean)

Scrolls through the document containing this element until the top or bottom edge of this element is aligned with the document's window.

SetAttribute(String, String) SetAttribute(String, String) SetAttribute(String, String) SetAttribute(String, String)

Sets the value of the named attribute on the element.

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

Returns a string that represents the current object.

(Inherited from Object)

Events

Click Click Click Click

Occurs when the user clicks on the element with the left mouse button.

DoubleClick DoubleClick DoubleClick DoubleClick

Occurs when the user clicks the left mouse button over an element twice, in rapid succession.

Drag Drag Drag Drag

Occurs when the user drags text to various locations.

DragEnd DragEnd DragEnd DragEnd

Occurs when a user finishes a drag operation.

DragLeave DragLeave DragLeave DragLeave

Occurs when the user is no longer dragging an item over this element.

DragOver DragOver DragOver DragOver

Occurs when the user drags text over the element.

Focusing Focusing Focusing Focusing

Occurs when the element first receives user input focus.

GotFocus GotFocus GotFocus GotFocus

Occurs when the element has received user input focus.

KeyDown KeyDown KeyDown KeyDown

Occurs when the user presses a key on the keyboard.

KeyPress KeyPress KeyPress KeyPress

Occurs when the user presses and releases a key on the keyboard.

KeyUp KeyUp KeyUp KeyUp

Occurs when the user releases a key on the keyboard.

LosingFocus LosingFocus LosingFocus LosingFocus

Occurs when the element is losing user input focus.

LostFocus LostFocus LostFocus LostFocus

Occurs when the element has lost user input focus.

MouseDown MouseDown MouseDown MouseDown

Occurs when the user presses a mouse button.

MouseEnter MouseEnter MouseEnter MouseEnter

Occurs when the user first moves the mouse cursor over the current element.

MouseLeave MouseLeave MouseLeave MouseLeave

Occurs when the user moves the mouse cursor off of the current element.

MouseMove MouseMove MouseMove MouseMove

Occurs when the user moves the mouse cursor across the element.

MouseOver MouseOver MouseOver MouseOver

Occurs when the mouse cursor enters the bounds of the element.

MouseUp MouseUp MouseUp MouseUp

Occurs when the user releases a mouse button.

Operators

Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement) Equality(HtmlElement, HtmlElement)

Compares two elements for equality.

Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement) Inequality(HtmlElement, HtmlElement)

Compares two HtmlElement objects for inequality.

Applies to