我的这个源代码放在了字符串String html里了~
怎么让WebBrowser控件解析我提供的这个html源代码

解决方案 »

  1.   

    webBrowser1.DocumentText =
            "<html><body>Please enter your name:<br/>" +
            "<input type='text' name='userName'/><br/>" +
            "<a href='http://www.microsoft.com'>continue</a>" +
            "</body></html>";
      

  2.   

    如果要使用字符串处理工具操作显示在 WebBrowser 控件中的 HTML 页的内容,则使用该属性。例如,可以使用该属性从数据库加载页,也可以使用正则表达式分析页。设置该属性时,WebBrowser 控件在加载指定文本之前,自动导航到 about:blank URL。这意味着设置该属性时,发生 Navigating、Navigated 和 DocumentCompleted 事件,且 Url 属性的值不再有意义。若要将网页的内容作为 Stream 访问,请使用 DocumentStream 属性。也可以通过 Document 属性,使用 HTML 文档对象模型 (DOM) 访问页内容。
    下面的代码示例演示如何使用 DocumentText 属性以编程方式显示选择的文档内容。此示例要求窗体包含一个名为 webBrowser1 的 WebBrowser 控件。
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentText =
            "<html><body>Please enter your name:<br/>" +
            "<input type='text' name='userName'/><br/>" +
            "<a href='http://www.microsoft.com'>continue</a>" +
            "</body></html>";
        webBrowser1.Navigating += 
            new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
    }private void webBrowser1_Navigating(object sender, 
        WebBrowserNavigatingEventArgs e)
    {
        System.Windows.Forms.HtmlDocument document =
            this.webBrowser1.Document;    if (document != null && document.All["userName"] != null && 
            String.IsNullOrEmpty(
            document.All["userName"].GetAttribute("value")))
        {
            e.Cancel = true;
            System.Windows.Forms.MessageBox.Show(
                "You must enter your name before you can navigate to " +
                e.Url.ToString());
        }
    }