我用一个WebClient对象下载了一个网页的HTML代码,现在我想用这段html代码创建一个HtmlDocument对象但是问题是,HtmlDocument对象是不能用new的。那怎么可以在不通过WebBrowser的情况下,利用一段html代码得到与WebBrowser.Document相同的HtmlDocument对象呢?
简单地说,平时是这样得到的,HtmlDocument = WebBrowser.Document;但现在必须不通过WebBrowser来实现,我已经用WebClient下载了html代码,问题就是不知道怎么生成HtmlDocument 对象
简单地说,平时是这样得到的,HtmlDocument = WebBrowser.Document;但现在必须不通过WebBrowser来实现,我已经用WebClient下载了html代码,问题就是不知道怎么生成HtmlDocument 对象
{
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());
}
}
Html Agility Pack ── 一个分析HTML的工具
http://blog.joycode.com/saucer/archive/2006/10/18/85316.aspx
tidy
sgmlreader
Html Agility Pack 还有别的,我记不得了
好像没有别的方法创建 HtmlDocument
http://msdn2.microsoft.com/zh-cn/library/microsoft.visualstudio.testtools.webtesting.htmldocument.htmldocument.aspx
再调用函数去用WebBrowser.Navigate的话,会出很多问题而且WebBrowser.Navigate并不会停止等待导航完毕,而是直接就去处理下一条语句了。所以一般要用WebClient才能实现,但是我用WebClient得到的只是html
Reference
http://www.samspublishing.com/articles/article.asp?p=27219&seqNum=8&rl=1
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/browser/walkall/default.asp