HtmlElement.Children Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene un oggetto HtmlElementCollection di tutti gli elementi figlio dell'elemento corrente.
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
Valore della proprietà
Raccolta di tutti gli HtmlElement oggetti con l'elemento corrente come elemento padre.
Esempio
Nell'esempio di codice seguente viene esaminato un documento HTML arbitrario e viene derivata una stringa che descrive gli elementi, con rientro e numeri di livello usati per indicare in che modo gli elementi sono annidati in profondità nel documento. A tale scopo, eseguire una ricerca nella Children raccolta di tutti gli elementi in modo ricorsivo, a partire dall'elemento HTML nella parte superiore del documento. Questo esempio di codice richiede che l'applicazione abbia un WebBrowser controllo denominato 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
Commenti
Molti degli elementi all'interno di un file HTML possono avere altri elementi HTML sottostanti. La Children raccolta fornisce un meccanismo semplice per esplorare la struttura ad albero di un documento.
Children espone solo gli elementi il cui padre diretto è l'elemento corrente. Se si dispone di un oggetto HtmlElement per un TABLE elemento , Children verranno visualizzati tutti gli TR elementi (riga) all'interno di TABLE. Per recuperare gli TD elementi (cella) contenuti all'interno degli TR elementi, è necessario utilizzare la Children raccolta in ogni singolo TR elemento oppure usare la All raccolta in HtmlElement.
Non è garantito che gli elementi in questa raccolta siano nell'ordine di origine.
Se CanHaveChildren è false, Children sarà sempre vuoto.