Take the 2-minute tour ×

What's up with that? It seems the only way to get an working HtmlDocument object is copying the Document property of an mshtml/webbrowser control. But spawning that is sloooooooooooow. I'd like to avoid writing my own HTML parser and HtmlAgilityPack is copyleft.

Are there other sources of getting an instantiated HtmlDocument that I can dump HTML from a string into?

Or, is there a way to override HtmlElement's annoying habit of throwing a fit when using InnerHtml/OuterHtml with img tags and tr elements?

Edit: I'm referring to System.Windows.Forms.HtmlDocument. My apologies, I'm still new to C# and .Net and know very little about COM and some of the other things this topic brings up.

share|improve this question
it would help if you said which HtmlDocument you are using by including the namespace or library name. –  Jonathan Allen Mar 27 '09 at 22:16
add comment

2 Answers

up vote 6 down vote accepted

It has no constructor because it's just a wrapper class around an unmanaged object.

Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.aspx

HtmlDocument provides a managed wrapper around Internet Explorer's document object, also known as the HTML Document Object Model (DOM). You obtain an instance of HtmlDocument through the Document property of the WebBrowser control.

Depending on what you want it for, you may want to look at SGMLReader or the up-to-date community version.

share|improve this answer
Thanks for the tip on SGMLReader. I was able to work around this by reading my HTML into SGMLReader, converting it to an XML document, and then injecting that code into the mshtml.HTMLDocument. THank you! –  Tom Corelis Mar 28 '09 at 5:36
add comment

Robust Programming?

When using the DOM through the WebBrowser control, you should always wait until the DocumentCompleted event occurs before attempting to access the Document property of the WebBrowser control. The DocumentCompleted event is raised after the entire document has loaded; if you use the DOM before then, you risk causing a run-time exception in your application.


share|improve this answer
add comment

Your Answer


