HtmlDocument
Class
定義
-
Assembly: System.Windows.Forms.dll -
C# public sealed class HtmlDocument
示例
下面的代碼示例使用 Northwind 數據庫中的數據來創(chuàng)建HTML TABLE 動態(tài)使用CreateElement。 AppendChild方法還使用,首先要添加的單元格 (TD 元素) 到行 (TR 元素),然后將行添加到表中,最后,將表追加到當前文檔的末尾。 代碼示例要求您的應用程序具有WebBrowser名為控件WebBrowser1 。 加載文檔之后,應調用代碼。 C# private void DisplayCustomersTable(){
DataSet customersSet = new DataSet();
DataTable customersTable = null;
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Customers", "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
sda.Fill(customersTable);
customersTable = customersSet.Tables[0];if (webBrowser1.Document != null)
{
HtmlElement tableRow = null;
HtmlElement headerElem = null;
HtmlDocument doc = webBrowser1.Document;
HtmlElement tableElem = doc.CreateElement("TABLE");
doc.Body.AppendChild(tableElem);
HtmlElement tableHeader = doc.CreateElement("THEAD");
tableElem.AppendChild(tableHeader);
tableRow = doc.CreateElement("TR");
tableHeader.AppendChild(tableRow);foreach (DataColumn col in customersTable.Columns)
{
headerElem = doc.CreateElement("TH");
headerElem.InnerText = col.ColumnName;
tableRow.AppendChild(headerElem);
}// Create table rows.HtmlElement tableBody = doc.CreateElement("TBODY");
tableElem.AppendChild(tableBody);foreach (DataRow dr in customersTable.Rows)
{
tableRow = doc.CreateElement("TR");
tableBody.AppendChild(tableRow);foreach (DataColumn col in customersTable.Columns)
{
Object dbCell = dr[col];
HtmlElement tableCell = doc.CreateElement("TD");if (!(dbCell is DBNull))
{
tableCell.InnerText = dbCell.ToString();
}
tableRow.AppendChild(tableCell);
}
}
}
}
注解
HtmlDocument 提供 Internet Explorer 文檔對象,也稱為 HTML 文檔對象模型 (DOM) 的托管的包裝。 獲取的實例HtmlDocument通過Document屬性的WebBrowser控件。 在 HTML 文檔內部的 HTML 標記可以嵌套在另一個。 HtmlDocument 因此表示其子級是實例的文檔樹的HtmlElement類。 下面的代碼示例顯示了一個簡單的 HTML 文件。 <HTML>
<BODY>
<DIV name="Span1">Simple HTML Form</DIV>
<FORM>
<SPAN name="TextLabel">Enter Your Name:</SPAN>
<INPUT type="text" size="20" name="Text1">
</FORM>
</BODY>
</HTML>
在此示例中,HtmlDocument表示整個文檔內的HTML 標記。 BODY , DIV ,FORM 并SPAN 標記由個人HtmlElement對象。 有幾種方法可以訪問此樹中的元素。 使用Body屬性來訪問BODY 標記和所有子項。 ActiveElement屬性可讓您HtmlElement上具有用戶輸入的焦點的 HTML 頁面元素。 HTML 頁中的所有元素可以都有一個名稱;All集合提供對每個訪問HtmlElement使用其名稱作為索引。 GetElementsByTagName 將返回HtmlElementCollection的所有HtmlElement對象使用給定的 HTML 標記名稱,如DIV 或TABLE 。 GetElementById 將返回單個HtmlElement對應于你提供的唯一 ID。 GetElementFromPoint 將返回HtmlElement可提供的鼠標指針坐標在屏幕上找到。 此外可以使用Forms和Images集合進行循環(huán)訪問元素表示用戶分別輸入窗體和圖形。 HtmlDocument 基于由 Internet Explorer 的 DHTML DOM 實現的非托管接口: IHTMLDocument , IHTMLDocument2 , IHTMLDocument3 ,和IHTMLDocument4 。 僅最常用的屬性和方法在這些非托管接口上的公開的HtmlDocument。 您可以訪問所有其他屬性和方法直接使用DomDocument屬性,您可以強制轉換為所需的非托管的接口指針。 一個 HTML 文檔可能包含不同的窗口的幀內的WebBrowser控件。 每個幀將顯示其自身 HTML 頁。 Frames集合是可通過Window屬性。 此外可以使用Window屬性來調整大小所顯示的頁面,滾動文檔,或顯示警報,并向用戶提示。 HtmlDocument 公開可能希望處理托管 HTML 頁面時的最常見事件。 對于不直接通過該接口公開的事件,可以添加的事件使用的處理程序AttachEventHandler。 HTML 文件可能包含SCRIPT 封裝在一個活動腳本語言,如 JScript 或 VBScript 編寫的代碼的標記。 InvokeScript方法用于執(zhí)行屬性和方法中定義SCRIPT 標記。 備注 盡管大多數屬性、 方法和事件上HtmlDocument具有保留相同的名稱,因為它們對非托管的 DOM 與某些已更改為與一致.NET Framework。
屬性
方法
事件
操作員
適用于.NET Core3.0 Preview 2 .NET Framework4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0
另請參閱
|