Using mshtml doesn't work
Asked Answered
T

4

21

I have a c# app and i have tried using some mshtml elements. But i have a problem. The using mshtml; namespace gives me a error is Visual Studio 2012.

Here is my source code,

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

But i can't use the functions of htmlelement, IHTMLElementCollection . The compiler gives me a error.

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?)

Tai answered 28/3, 2014 at 15:59 Comment(8)
Is it showing up correctly in your project references?Mehitable
Is that the only error?Gooseherd
i have nstalled windows 8 4.5.0 sdk in my computer and i also have downloaded a mshtml.dll online and have added it to VS. But i still get the error. That is the only error i am getting for that script.Tai
You need to add a COM reference to Microsoft HTML object library.Counterpressure
Personally, I only added a reference to mshtml using the standard way (no COM component) and it worked. Remove your reference to your local mshtml.dll and search for mshtml in the add reference dialogSubtemperate
@LouG I can't find it in my reference dialog.Tai
@stijn , Any help on how to do it ?Tai
Any particular reason you want to use mshtml, and not any of the HTML parser libraries, such as HtmlAgilityPack?Platy
S
41

Right click on References in your project in Solution Explorer. Then click Add Reference.... In Assemblies type in search 'HTML' and you'll see Microsoft.mshtml. Add this to your project and you could use HTMLDocument. Good luck

Saker answered 28/3, 2014 at 17:20 Comment(2)
@idlerboris I have the same issue. Opened "Add Reference..", clicked Assemblies, typed "html" into search and it says "No items found".Mckelvey
Search mshtml and you'll be provided with two options, the first one works just fine.Ryley
R
25

Microsoft.mshtml is in COM tab in Reference Manager and it's named "Microsoft HTML Object Library".

Right answered 8/1, 2016 at 7:32 Comment(0)
B
0

I want to add that we had several times the problem, that the needed namespace switched from mshtml to MSHTML and back. So even if you have the reference added and all looks good, check if the namespace changed due to an updated library.

Brander answered 25/10, 2019 at 9:5 Comment(0)
T
0

mshtml changed to MSHTML. So after you did this:

"Microsoft.mshtml is in COM tab in Reference Manager and it's named "Microsoft HTML Object Library"."

change mshtml to MSHTML in your code.

Trap answered 14/3, 2021 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.