使用mshtml不起作用
本文关键字:不起作用 mshtml 使用 | 更新日期: 2023-09-27 17:54:27
我有一个c#应用程序,我尝试使用一些mshtml元素。但我有个问题。using mshtml;
命名空间给我一个错误是Visual Studio 2012。
这是我的源代码,
namespace Tagger
{
using mshtml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
public class HTMLForm
{
private string _action = "";
private string _method = "";
public Hashtable Inputs = new Hashtable();
public HTMLForm(IHTMLFormElement element)
{
this._method = element.method;
this._action = element.action;
foreach (IHTMLInputElement element2 in (IHTMLElementCollection) element.tags("input"))
{
try
{
string name = element2.name;
string str2 = element2.value;
if (name == null)
{
name = element2.type;
}
this.Inputs.Add(name, str2);
}
catch
{
}
}
}
public static HTMLForm[] GetAllForms(string html)
{
List<HTMLForm> list = new List<HTMLForm>();
HTMLDocument document = (HTMLDocument) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("25336920-03F9-11CF-8FD0-00AA00686F13")));
document.write(new object[] { html });
document.close();
foreach (IHTMLFormElement element in document.forms)
{
list.Add(new HTMLForm(element));
}
return list.ToArray();
}
public static HTMLForm GetFormByAction(string html, string action)
{
foreach (HTMLForm form in GetAllForms(html))
{
if (form.Action.ToLower() == action.ToLower())
{
return form;
}
}
return null;
}
public string ToPostData()
{
StringBuilder builder = new StringBuilder();
foreach (string str in this.Inputs.Keys)
{
object obj2 = this.Inputs[str];
string str2 = (obj2 == null) ? "" : obj2.ToString();
builder.AppendFormat("{0}={1}&", HTTPBase.encode(str), HTTPBase.encode(str2));
}
if (builder.Length > 1)
{
return builder.ToString().Substring(0, builder.Length - 1);
}
return "";
}
public string Action
{
get
{
return this._action;
}
set
{
this._action = value;
}
}
public string Method
{
get
{
return this._method;
}
set
{
this._method = value;
}
}
}
}
但是我不能使用htmlelement, IHTMLElementCollection的函数。编译器给了我一个错误。
错误1无法找到类型或命名空间名称'mshtml'您缺少using指令或程序集引用吗?)
Error 5 The type or namespace name 'HTMLDocument' could not be found (are
您缺少using指令或程序集引用?
错误2无法找到类型或命名空间名称'IHTMLFormElement'缺少using指令或程序集引用?)
错误3类型或命名空间名称'IHTMLElementCollection'无法找到(您是否缺少using指令或程序集引用?)
错误4类型或命名空间名称'HTMLDocument'找不到(您是否缺少using指令或程序集引用?)
右键单击Solution Explorer
项目中的References
。单击"Add Reference...
"。在Assemblies
中输入搜索'HTML',你会看到Microsoft.mshtml
。将它添加到您的项目中,您可以使用HTMLDocument。祝你好运
Microsoft。mshtml在参考管理器的COM选项卡中,命名为"Microsoft HTML对象库"。
我想补充一下,我们遇到了好几次这样的问题,即所需的名称空间在mshtml和mshtml之间来回切换。因此,即使您已经添加了引用并且看起来很好,也要检查名称空间是否因更新库而更改。
mshtml更改为mshtml。在你这样做之后:
"微软。mshtml在参考管理器的COM选项卡中,命名为"Microsoft HTML Object Library"。