1 out of 1 rated this helpful - Rate this topic

HtmlElement Class

Represents an HTML element inside of a Web page.

System.Object
  System.Windows.Forms.HtmlElement

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public sealed class HtmlElement

The HtmlElement type exposes the following members.

  Name Description
Public property All Gets an HtmlElementCollection of all elements underneath the current element.
Public property CanHaveChildren Gets a value indicating whether this element can have child elements.
Public property Children Gets an HtmlElementCollection of all children of the current element.
Public property ClientRectangle Gets the bounds of the client area of the element in the HTML document.
Public property Document Gets the HtmlDocument to which this element belongs.
Public property DomElement Gets an unmanaged interface pointer for this element.
Public property Enabled Gets or sets whether the user can input data into this element.
Public property FirstChild Gets the next element below this element in the document tree.
Public property Id Gets or sets a label by which to identify the element.
Public property InnerHtml Gets or sets the HTML markup underneath this element.
Public property InnerText Gets or sets the text assigned to the element.
Public property Name Gets or sets the name of the element.
Public property NextSibling Gets the next element at the same level as this element in the document tree.
Public property OffsetParent Gets the element from which OffsetRectangle is calculated.
Public property OffsetRectangle Gets the location of an element relative to its parent.
Public property OuterHtml Gets or sets the current element's HTML code.
Public property OuterText Gets or sets the current element's text.
Public property Parent Gets the current element's parent element.
Public property ScrollLeft Gets or sets the distance between the edge of the element and the left edge of its content.
Public property ScrollRectangle Gets the dimensions of an element's scrollable region.
Public property ScrollTop Gets or sets the distance between the edge of the element and the top edge of its content.
Public property Style Gets or sets a comma-delimited list of styles for the current element.
Public property TabIndex Gets or sets the location of this element in the tab order.
Public property TagName Gets the name of the HTML tag.
Top
  Name Description
Public method AppendChild Adds an element to another element's subtree.
Public method AttachEventHandler Adds an event handler for a named event on the HTML Document Object Model (DOM).
Public method DetachEventHandler Removes an event handler from a named event on the HTML Document Object Model (DOM).
Public method Equals Tests if the supplied object is equal to the current element. (Overrides Object.Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Focus Puts user input focus on the current element.
Public method GetAttribute Retrieves the value of the named attribute on the element.
Public method GetElementsByTagName Retrieves a collection of elements represented in HTML by the specified HTML tag.
Public method GetHashCode Serves as a hash function for a particular type. (Overrides Object.GetHashCode().)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InsertAdjacentElement Insert a new element into the Document Object Model (DOM).
Public method InvokeMember(String) Executes an unexposed method on the underlying DOM element of this element.
Public method InvokeMember(String, Object[]) Executes a function defined in the current HTML page by a scripting language.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method RaiseEvent Causes the named event to call all registered event handlers.
Public method RemoveFocus Removes focus from the current element, if that element has focus.
Public method ScrollIntoView Scrolls through the document containing this element until the top or bottom edge of this element is aligned with the document's window.
Public method SetAttribute Sets the value of the named attribute on the element.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event Click Occurs when the user clicks on the element with the left mouse button.
Public event DoubleClick Occurs when the user clicks the left mouse button over an element twice, in rapid succession.
Public event Drag Occurs when the user drags text to various locations.
Public event DragEnd Occurs when a user finishes a drag operation.
Public event DragLeave Occurs when the user is no longer dragging an item over this element.
Public event DragOver Occurs when the user drags text over the element.
Public event Focusing Occurs when the element first receives user input focus.
Public event GotFocus Occurs when the element has received user input focus.
Public event KeyDown Occurs when the user presses a key on the keyboard.
Public event KeyPress Occurs when the user presses and releases a key on the keyboard.
Public event KeyUp Occurs when the user releases a key on the keyboard.
Public event LosingFocus Occurs when the element is losing user input focus.
Public event LostFocus Occurs when the element has lost user input focus.
Public event MouseDown Occurs when the user presses a mouse button.
Public event MouseEnter Occurs when the user first moves the mouse cursor over the current element.
Public event MouseLeave Occurs when the user moves the mouse cursor off of the current element.
Public event MouseMove Occurs when the user moves the mouse cursor across the element.
Public event MouseOver Occurs when the mouse cursor enters the bounds of the element.
Public event MouseUp Occurs when the user releases a mouse button.
Top
  Name Description
Public operator Static member Equality Compares two elements for equality.
Public operator Static member Inequality Compares two HtmlElement objects for inequality.
Top

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 + ")");
				returnStr.AppendLine(str.ToString());

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

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

			return(returnStr.ToString());
		}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ