HtmlElementCollection HtmlElementCollection HtmlElementCollection HtmlElementCollection Class


Defines a collection of HtmlElement objects.

public ref class HtmlElementCollection sealed : System::Collections::ICollection
public sealed class HtmlElementCollection : System.Collections.ICollection
type HtmlElementCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class HtmlElementCollection
Implements ICollection


The following code example uses HtmlElementCollection objects to print out a textual representation of the Document Object Model (DOM) of a page.

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

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 & ")")

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

        Str.Remove(0, Str.Length)

    PrintDom = ReturnStr.ToString()
End Function


Count Count Count Count

Gets the number of elements in the collection.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

Gets an item from the collection by specifying its numerical index.

Item[String] Item[String] Item[String] Item[String]

Gets an item from the collection by specifying its name.


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

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetElementsByName(String) GetElementsByName(String) GetElementsByName(String) GetElementsByName(String)

Gets a collection of elements by their name.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Returns an enumerator that iterates through a collection.

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

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

Copies the elements of the collection to an Array, starting at a particular Array index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

Gets a value indicating whether access to the HtmlElementCollection is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

Gets an object that can be used to synchronize access to the collection.

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to