HtmlElementCollection Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- Inheritance
-
HtmlElementCollection
- Implements
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 + ")");
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
Count |
Gets the number of elements in the collection. |
Item[Int32] |
Gets an item from the collection by specifying its numerical index. |
Item[String] |
Gets an item from the collection by specifying its name. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Gets a collection of elements by their name. |
Get |
Returns an enumerator that iterates through a collection. |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
To |
Returns a string that represents the current object. (Inherited from Object) |
ICollection. |
Copies the elements of the collection to an Array, starting at a particular Array index. |
ICollection. |
Gets a value indicating whether access to the HtmlElementCollection is synchronized (thread safe). |
ICollection. |
Gets an object that can be used to synchronize access to the collection. |
Cast<TResult>(IEnumerable) |
Casts the elements of an IEnumerable to the specified type. |
Of |
Filters the elements of an IEnumerable based on a specified type. |
As |
Enables parallelization of a query. |
As |
Converts an IEnumerable to an IQueryable. |
Product | Versions |
---|---|
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: