开发者

Can Webbrowser Control handle "bad" HTML?

I'm working with the Webbrowser control in C# and trying to access some HtmlElements in the document. The problem is that the document Body only contains two out of five children. (http://www.target.com/cart/ref=nav_sc_rev_checkout). So I can't access specific elements in the body, although it renders fine in开发者_运维百科 the UI.

I suspect that there is bad HTML in the Body so that the Document Tree is corrupt?

Is there a way to handle this, since it still renders nice..?

Thanks.

Update: The problem was that the DocumentCompleted event was triggered but the Doc was not fully parsed so that was why I only got 2 out of 5 elements.


Yes, the WebBrowser is a wrapper round IE and it will handle bad HTML as good as it can.


Can't you simply write the contents to a text file instead of a web browser control and make that into a HTML file. Then load it in your browser and inspect with the dev tool of your choice.


Beside the fact the html code of this site have more than 200 errors (mostly missing entities), you can try to load the code into an XmlDocument or XDocument inside your program and access the nodes you want via XPath.


If you need to programmatically interact with HTML and more specifically bad HTML I would suggest you to take a look at HTMLAgilityPack.

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜