你可以把
mshtml.HtmlDocument
到
IHTMLDocument2
接口,使主要对象的属性和方法可用:
var doc = (IHTMLDocument2)new mshtml.HTMLDocument();
或创建一个
HtmlDocumentClass
实例使用
Activator.CreateInstance()
使用类型guid,然后强制转换为
IHTMLDocument2
接口。
IHTMLDocument2 doc =
(IHTMLDocument2)Activator.CreateInstance(
Type.GetTypeFromCLSID(new Guid("25336920-03F9-11CF-8FD0-00AA00686F13")));
差不多是一样的。我比较喜欢第一个,主要是
for this reason
然后你可以写信给
HtmlDocument
无论你想要什么。例如:
doc.write(File.ReadAllText(@"[Some Html Page]"));
Console.WriteLine(doc.body.innerText);
要创建一个HTMLDocument,一个框架HTML页面就足够了,如下所示:
string html = "<!DOCTYPE html><html><head></head><Body><p></body></html>";
doc.write(html);
注意:在创建文档之前,页面中的所有元素都将
null
.
之后,您可以设置
Body.InnerHtml
其他方面:
doc.body.innerHTML = "<P>Some Text</P>";
Console.WriteLine(doc.body.innerText);
请注意,如果需要更广泛地使用HTML文档,则必须转换到更高级别的接口:
IHTMLDocument3
到
IHTMLDocument8
(从现在起),代替系统版本。
经典
getElementById
,
getElementsByName
,
getElementsByTagName
方法在
IHTML文档3
接口。
例如,使用
getElementsByTagName()
检索
InnerText
一个
HTMLElement
使用它的标签名:
string innerText =
(doc as IHTMLDocument3).getElementsByTagName("body")
.OfType<IHTMLElement>().First().inneâârText;
注释
:
如果你找不到
IHTMLDocument6
,
IHTMLDocument7
和
IHTML文档8
接口(以及可能在msdn文档中引用的其他接口),那么您可能在
\Windows\Assembly\
GAC
. 按照Hans Passant的建议创建新的
Interop.mshtml
图书馆:
How to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7?