C#:HtmlDocument 对象没有构造函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/691657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: HtmlDocument object has no constructor?
提问by Tom Corelis
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.
那是怎么回事?似乎获得工作 HtmlDocument 对象的唯一方法是复制 mshtml/webbrowser 控件的 Document 属性。但是产卵是slooooooooooooow。我想避免编写自己的 HTML 解析器,而 HtmlAgilityPack 是 copyleft。
Are there other sources of getting an instantiated HtmlDocument that I can dump HTML from a string into?
是否有其他获取实例化 HtmlDocument 的来源,我可以将 HTML 从字符串转储到其中?
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?
或者,有没有办法覆盖 HtmlElement 在使用带有 img 标签和 tr 元素的 InnerHtml/OuterHtml 时抛出合适的恼人习惯?
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.
编辑:我指的是 System.Windows.Forms.HtmlDocument。抱歉,我还是 C# 和 .Net 的新手,对 COM 和本主题提出的其他一些内容知之甚少。
采纳答案by tvanfosson
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
参考: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.
HtmlDocument 为 Internet Explorer 的文档对象(也称为 HTML 文档对象模型 (DOM))提供了一个托管包装器。您可以通过 WebBrowser 控件的 Document 属性获取 HtmlDocument 的实例。
Depending on what you want it for, you may want to look at SGMLReaderor the up-to-date community version.
根据您的需求,您可能需要查看SGMLReader或最新的社区版本。
回答by Jungchan
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.
通过 WebBrowser 控件使用 DOM 时,应始终等到 DocumentCompleted 事件发生,然后再尝试访问 WebBrowser 控件的 Document 属性。DocumentCompleted 事件在整个文档加载后引发;如果您在此之前使用 DOM,您可能会在应用程序中引发运行时异常。