Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Propiedad HtmlElement.Children

 

Publicado: octubre de 2016

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

Espacio de nombres:   System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)

public HtmlElementCollection Children { get; }

Valor de propiedad

Type: System.Windows.Forms.HtmlElementCollection

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

Many of the elements inside of an HTML file can have other HTML elements underneath them. The P:System.Windows.Forms.HtmlElement.Children collection provides a simple mechanism for exploring the tree structure of a document.

Children only exposes elements whose direct parent is the current element. If you have an HtmlElement for a TABLE element, Children will give you all of the TR (row) elements inside of the TABLE. To retrieve the TD (cell) elements contained inside of the TR elements, you will need to use either the Children collection on each individual TR element, or use the All collection on HtmlElement.

Elements in this collection are not guaranteed to be in source order.

If P:System.Windows.Forms.HtmlElement.CanHaveChildren is false, Children will always be empty.

The following code example examines an arbitrary HTML document and derive a string describing the elements, with indentation and level numbers used to indicate how deeply nested the elements are in the document. It does this by searching the Children collection of all elements recursively, starting with the HTML element at the top of the document. This code example requires that your application has a T:System.Windows.Forms.WebBrowser control named 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());
}

.NET Framework
Disponible desde 2.0
Volver al principio
Mostrar: