HtmlElement.Children 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
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 대한 TABLE 항목이 있는 경우 내부에 있는 모든 Children (행) 요소를 TR제공합니다. TABLE 요소 내부에 TD 포함된 (셀) 요소를 검색 TR 하려면 각 개별 Children 요소의 컬렉션을 사용 TR 하거나 컬렉션을 All사용해야 HtmlElement 합니다.
이 컬렉션의 요소는 원본 순서로 보장되지 않습니다.
이 CanHaveChildrenfalse 경우 Children 항상 비어 있습니다.