2014-03-28 71 views
14

我有一个c#应用程序,我曾尝试使用一些mshtml元素。但我有一个问题。该using mshtml;命名空间给了我一个错误的Visual Studio 2012使用mshtml不起作用

这里是我的源代码,

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; 
      } 
     }   
    } 
} 

但我不能使用HTML元素,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指令或程序集引用?)

+1

它是否正确显示在您的项目参考? – DaveParsons

+0

这是唯一的错误? –

+0

我已经在我的电脑上安装了windows 8 4.5.0 sdk,我也在网上下载了一个mshtml.dll并将其添加到VS.但我仍然得到错误。那是我得到的唯一错误。 – kks21199

回答

25

右键点击References在您的项目中Solution Explorer。然后点击Add Reference...。在Assemblies键入搜索'HTML',你会看到Microsoft.mshtml。将此添加到您的项目中,您可以使用HTMLDocument。祝你好运

+0

“找不到任何项目。” – user169771

+0

@ user169771,在哪一步你得到这个错误? – idlerboris

+0

@idlerboris我有同样的问题。打开“添加引用..”,单击组件,键入“html”进入搜索,并显示“没有找到任何项目”。 – VenerableAgents

13

Microsoft.mshtml在引用管理器中的COM选项卡中,它被命名为“Microsoft HTML Object Library”。