把String转换为HtmlDocment,不能用webBrowser!

解决方案 »

  1.   

    MSDN帮助 中 明确的显示这个类 只能是 使用 webBrowser
    ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/T_System_Windows_Forms_HtmlDocument.htm注意:此类在 .NET Framework 2.0 版中是新增的。 提供对 WebBrowser 控件承载的 HTML 文档的顶级编程访问。 命名空间:System.Windows.Forms
    程序集:System.Windows.Forms(在 system.windows.forms.dll 中)语法
    Visual Basic(声明) 
    Public NotInheritable Class HtmlDocument
     
    Visual Basic(用法) 
    Dim instance As HtmlDocument 
    C# 
    public sealed class HtmlDocument
     
    C++ 
    public ref class HtmlDocument sealed
     
    J# 
    public final class HtmlDocument
     
    JScript 
    public final class HtmlDocument
     备注
    HtmlDocument 为 Internet Explorer 的文档对象提供托管包装,该文档对象也称为 HTML 文档对象模型 (DOM)。您可以通过 WebBrowser 控件的 Document 属性获取 HtmlDocument 的实例。 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 属性提供 HTML 页上具有用户输入焦点的元素的 HtmlElement。HTML 页中的所有元素都可以有一个名称;All 集合将元素的名称用作索引来提供对每个 HtmlElement 的访问。GetElementsByTagName 将返回具有给定 HTML 标记名称(如 DIV 或 TABLE)的所有 HtmlElement 对象的 HtmlElementCollection。GetElementById 将返回对应于所提供的唯一 ID 的单个 HtmlElement。GetElementFromPoint 将返回可以在屏幕上所提供的鼠标指针坐标位置找到的 HtmlElement。 您也可以分别使用 Forms 和 Images 集合来循环访问表示用户输入窗体和图形的元素。 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 方法用于执行 SCRIPT 标记中定义的属性和方法。 注意 
    虽然 HtmlDocument 中的大部分属性、方法和事件的名称都与非托管 DOM 上的对应项的名称相同,但为了与 .NET Framework 保持一致,某些名称已发生了改变。 
     示例
    下面的代码示例通过 CreateElement,使用 Northwind 数据库中的数据动态创建一个 HTML TABLE。此外,还使用 AppendChild 方法,首先向行(TR 元素)中添加单元格(TD 元素),然后向表中添加行,最后将表追加到当前文档的末尾。该代码示例要求应用程序具有一个称为 WebBrowser1 的 WebBrowser 控件。Visual Basic  复制代码 
    Private Sub DisplayCustomersTable()
        ' Initialize the database connection.
        Dim CustomerData As New DataSet()
        Dim CustomerTable As DataTable    Try
            Dim DBConn As New SqlConnection("Data Source=CLIENTUE;Integrated Security=SSPI;Initial Catalog=Northwind;")
            Dim DBQuery As New SqlDataAdapter("SELECT * FROM CUSTOMERS", DBConn)
            DBQuery.Fill(CustomerData)
        Catch dbEX As DataException    End Try    CustomerTable = CustomerData.Tables("Customers")    If (Not (WebBrowser1.Document Is Nothing)) Then
            With WebBrowser1.Document
                Dim TableElem As HtmlElement = .CreateElement("TABLE")
                .Body.AppendChild(TableElem)            Dim TableRow As HtmlElement            ' Create the table header. 
                Dim TableHeader As HtmlElement = .CreateElement("THEAD")
                TableElem.AppendChild(TableHeader)
                TableRow = .CreateElement("TR")
                TableHeader.AppendChild(TableRow)            Dim HeaderElem As HtmlElement
                For Each Col As DataColumn In CustomerTable.Columns
                    HeaderElem = .CreateElement("TH")
                    HeaderElem.InnerText = Col.ColumnName
                    TableRow.AppendChild(HeaderElem)
                Next            ' Create table rows.
                Dim TableBody As HtmlElement = .CreateElement("TBODY")
                TableElem.AppendChild(TableBody)
                For Each Row As DataRow In CustomerTable.Rows
                    TableRow = .CreateElement("TR")
                    TableBody.AppendChild(TableRow)
                    For Each Col As DataColumn In CustomerTable.Columns
                        Dim Item As Object = Row(Col)
                        Dim TableCell As HtmlElement = .CreateElement("TD")
                        If Not (TypeOf (Item) Is DBNull) Then
                            TableCell.InnerText = CStr(Item)
                        End If
                        TableRow.AppendChild(TableCell)
                    Next
                Next        End With
        End If
    End Sub 
    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);
                }
            }
        }
    } 继承层次结构
    System.Object 
      System.Windows.Forms.HtmlDocument线程安全
    此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
    平台
    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.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。版本信息
    .NET Framework
    受以下版本支持:2.0
      

  2.   

    WebBrowser webb = new WebBrowser();
    webb.Navigate("about:blank");
    HtmlDocument htmldoc = webb.Document.OpenNew(true);
    htmldoc.Write(strWeb);只能这样!