Export (0) Print
Expand All

HtmlElement.Children Property

Gets an HtmlElementCollection of all children of the current element.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public HtmlElementCollection Children { get; }

Property Value

Type: System.Windows.Forms.HtmlElementCollection
A collection of all HtmlElement objects that have the current element as a parent.

Many of the elements inside of an HTML file can have other HTML elements underneath them. The 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 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 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

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft