Export (0) Print
Expand All

HtmlElement.Children Property

Note: This property is new in the .NET Framework version 2.0.

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 */
public HtmlElementCollection get_Children ()

public function get Children () : HtmlElementCollection

Property Value

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());
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft