HtmlElement Class
Represents an HTML element inside of a Web page.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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 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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.