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 則會提供 內 TABLE 的所有 TR (列) 元素。 若要擷 TD 取元素內 TR 所含的 (儲存格) 元素,您必須在每個 Children 個別 TR 元素上使用集合,或在 All 上使用 HtmlElement 集合。

此集合中的元素不保證為來源順序。

如果 CanHaveChildrenfalseChildren 則一律是空的。

適用於

另請參閱