HtmlElement.Children Propiedad

Definición

Obtiene una colección HtmlElementCollection de todos los elementos secundarios del elemento actual.

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

Valor de propiedad

HtmlElementCollection

Colección de todos los objetos HtmlElement cuyo elemento primario es el elemento actual.

Ejemplos

En el ejemplo de código siguiente se examina un documento HTML arbitrario y se deriva una cadena que describe los elementos, con sangría y números de nivel usados para indicar la profundidad anidada de los elementos en el documento. Para ello, busca en la Children colección de todos los elementos de forma recursiva, empezando por el elemento HTML en la parte superior del documento. Este ejemplo de código requiere que la aplicación tenga un WebBrowser control denominado 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

Comentarios

Muchos de los elementos dentro de un archivo HTML pueden tener otros elementos HTML debajo de ellos. La Children colección proporciona un mecanismo sencillo para explorar la estructura de árbol de un documento.

Children solo expone los elementos cuyo elemento primario directo es el elemento actual. Si tiene un HtmlElement para un TABLE elemento, Children le proporcionará todos los TR elementos (row) dentro de TABLE. Para recuperar los TD elementos (celda) contenidos dentro de los TR elementos , deberá usar la Children colección en cada elemento individual TR o usar la All colección en HtmlElement.

No se garantiza que los elementos de esta colección estén en orden de origen.

Si CanHaveChildren es false, Children siempre estará vacío.

Se aplica a

Consulte también