HtmlElementCollection 類別

定義

定義 HtmlElement 物件的集合。

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
繼承
HtmlElementCollection
實作

範例

下列程式碼範例會使用 HtmlElementCollection 物件來列印頁面之 Document Object Model (DOM) 的文字表示。

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

取得集合中的項目數。

Item[Int32]

藉由指定項目的數值索引從集合中取得項目。

Item[String]

藉由指定項目的名稱從集合中取得項目。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetElementsByName(String)

依據項目名稱取得項目的集合。

GetEnumerator()

傳回逐一查看集合的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的 Array 索引開始,將集合的項目複製至 Array

ICollection.IsSynchronized

取得值,這個值表示對 HtmlElementCollection 的存取是否同步 (安全執行緒)。

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於