定义 HtmlElement 对象的集合。
命名空间: System.Windows.Forms
程序集: System.Windows.Forms(在 System.Windows.Forms.dll 中)
下面的代码示例使用 HtmlElementCollection 对象打印一页的文档对象模型 (DOM) 的文本表示形式。
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());
}