HtmlElement Class

Note: This class is new in the .NET Framework version 2.0.

Represents an HTML element inside of a Web page.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in

Public NotInheritable Class HtmlElement
Dim instance As HtmlElement

public final class HtmlElement
public final class HtmlElement

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.

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 + ")");

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

        str.Remove(0, str.Length);



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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions