我在使用WebBrowser控件包含/提供的HtmlDocument对象时遇到问题。
似乎当我使用GetElementsByTagName("ol")
时,返回的结果实际上是WebBrowser.DocumentText
属性包含的精简版本。是否有一个原因?我做错了什么来获取元素?
以下是一个例子:
string html = "<html><head></head><body><ol type=\"i\"><li>SomeItem</li></ol></body></html>";
//this part allows me to load the HTML into the HtmlDocument of the WebBrowser control,
// without having to render the control on a form
this.webBrowser1.Navigate("about:blank");
this.webBrowser1.Document.Write(html);
//then i get the document object in order to work with the elements properly.
HtmlDocument doc = this.webBrowser1.Document;
//then i get the lists contained in that document... and play with those.
HtmlElementCollection ol_lists = doc.GetElementsByTagName("ol");
foreach(HtmlElement ol in ol_lists)
{
string type = ol.GetAttribute("type");
}
此代码示例实际上在type
:
""
然后,如果我这样做是为了看看为什么它实际上是空的:
string outer_html = ol.OuterHtml;
我得到以下结果:
<OL><LI>SomeItem</LI></OL>
这显然不是网络浏览器中的内容...因为如果我这样做:
string text = this.webBrowser1.DocumentText;
我得到了这个结果:
<html><head></head><body><ol type="i"><li>SomeItem</li></ol></body></html>
如果我让代码运行并将HTML内容显示到浏览器控件中,它实际上会显示一个带有罗马数字的列表作为其项目...所以很明显我的html代码在那里确定了...什么是我做错了?如何从HtmlDocument对象中获取实际的元素属性和其他所有内容?