我现在的目的是想创建一个HtmlDocument对象,而我只有一段<html>....</html>格式的字符串。想创建的HtmlDocuemnt对象的内容为这段字符串,现在的问题是HtmlDocument对象实例是用WebBrowser.Document属性来得到的,但是我的界面中没有添加WebBrowser控件,如果新实例化一个WebBrowser类的话再得出他的Document值是null;
   设置了WebBrowser.DocumentText="<html>...</html>";后WebBrowser.DocumentText的值还是"<html></html>"内容为空,谁有比较好的方法利用现在有的Html代码字符串来创建一个HtmlDocument对象。

解决方案 »

  1.   

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