語言

HtmlElement.Children 屬性

定義

獲得 HtmlElementCollection 當前元素的所有子嗣之一。

public:
 property System::Windows::Forms::HtmlElementCollection ^ Children { System::Windows::Forms::HtmlElementCollection ^ get(); };
public System.Windows.Forms.HtmlElementCollection Children { get; }
member this.Children : System.Windows.Forms.HtmlElementCollection
Public ReadOnly Property Children As HtmlElementCollection

屬性值

一組包含 HtmlElement 所有以當前元素為父元素的物件。

範例

以下程式碼範例檢視一個任意的 HTML 文件,並推導出描述元素的字串,並以縮排與層級編號表示元素在文件中巢狀的深度。 它透過遞迴搜尋 Children 所有元素的集合來達成此目的,從文件頂端的 HTML 元素開始。 此程式碼範例要求你的應用程式有一個 WebBrowser 名為 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());
}
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

備註

HTML 檔案中的許多元素底下可以有其他 HTML 元素。 這個 Children 集合提供了一個簡單的機制來探索文件的樹狀結構。

Children 只暴露直接父元素為當前元素的元素。 如果你有一個HtmlElement元素,TABLEChildren則會得到 中所有TR(列)元素。TABLE 要取得 TD 元素內 TR 包含的(單元)元素,你需要對 Children 每個元素 TR 使用集合,或在 上使用 All 集合 HtmlElement。

本集合中的元素不保證是按來源順序排列。

若 CanHaveChildren 是 ,falseChildren則 永遠為空。

適用於

另請參閱