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

속성 값

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 대한 항목이 TABLE 있는 경우 해당 요소 ChildrenTABLETR 모든 (행) 요소를 제공합니다. 요소 내에 포함된 (셀) 요소를 검색 TD 하려면 각 개별 TR 요소의 Children TR 컬렉션을 사용하거나 컬렉션을 HtmlElement사용해야 All 합니다.

이 컬렉션의 요소는 원본 순서로 보장되지 않습니다.

false``ChildrenCanHaveChildren 항상 비어 있습니다.

적용 대상

추가 정보