gpt4 book ai didi

c# - 使用 mshtml 不起作用

转载 作者:可可西里 更新时间:2023-11-01 03:03:47 33 4
gpt4 key购买 nike

我有一个 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 的功能。编译器给我一个错误。

Error 1 The type or namespace name 'mshtml' could not be found (are you missing a using directive or an assembly reference?)

Error 5   The type or namespace name 'HTMLDocument' could not be found (are 

you missing a using directive or an assembly reference?

Error 2 The type or namespace name 'IHTMLFormElement' could not be found (are you missing a using directive or an assembly reference?)

Error 3 The type or namespace name 'IHTMLElementCollection' could not be found (are you missing a using directive or an assembly reference?)

Error 4 The type or namespace name 'HTMLDocument' could not be found (are you missing a using directive or an assembly reference?)

最佳答案

Solution Explorer 中右键单击项目中的 References。然后单击 Add Reference...。在 Assemblies 中搜索“HTML”,您将看到 Microsoft.mshtml。将它添加到您的项目中,您就可以使用 HTMLDocument。祝你好运

关于c# - 使用 mshtml 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22717704/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com