Подтвердить что ты не робот

Использование mshtml не работает

У меня есть приложение С#, и я попытался использовать некоторые элементы 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 или ссылки на сборку?)

4b9b3361

Ответ 1

Щелкните правой кнопкой мыши по References в вашем проекте в Solution Explorer. Затем нажмите Add Reference.... В Assemblies введите "HTML", и вы увидите Microsoft.mshtml. Добавьте это в свой проект, и вы можете использовать HTMLDocument. Удачи.

Ответ 2

Microsoft.mshtml находится на вкладке COM в диспетчере ссылок и называется "Библиотека объектов Microsoft HTML".