VB.NET / C#.Net MSHTML: Unable to get "name" attribute from Outerhtml after using "setAttribute('name',value)" for certain elements

StackOverflow https://stackoverflow.com/questions/14995638

Question

I am developing a WYSIWYG application specifically for my company usage with custom integration with company's existing tools.

I was unable to get the "name" attribute out of certain elements when trying to get the html string by using ".OuterHtml", especially INPUT tag element.

Code example:

    `Dim inElem as windows.forms.htmlElement = hdoc.CreateElement("INPUT")`
    `inElem.Id = "txt01"`
    `inElem.setAttribute("name", inElem.Id)`
    `inElem.setAttribute("type", "text")`
    `inElem.setAttribute("placeholder","text here....")`

    '' append the created element to html body
    `hdoc.Body.AppendChild(inElem)`

    --> Getting html string:
        ** hdoc.body.getElementById("txt01").OuterHtml => "<input id=txt01 placeholder='text here....'></input>"
    --> What I really want is:
        ** hdoc.body.getElementById("txt01").OuterHtml => "<input id=txt01 placeholder='text here....' type='text' name='txt01'></input>"

Yes, not only name attribute were missing, some other too. (e.g. TYPE) Anyone could help me on this matter?

Solution attempted:

    For Each inputEle As Windows.Forms.HtmlElement In hdoc.Body.GetElementsByTagName("input")
        CType(inputEle.DomElement, mshtml.IHTMLInputElement).name = inputEle.Id
    Next

** FAILED ** :(

ULTIMATE SOLUTION:

    Use HTML Agility Pack:
    ----------------------
    Dim inputEle3 As HtmlAgilityPack.HtmlNode = new_wb.CreateElement("input")
    inputEle3.Attributes.Add("id", "txt01")
    inputEle3.Attributes.Add("name", inputEle3.Id)
    inputEle3.Attributes.Add("type", "text")
    inputEle3.Attributes.Add("placeholder", "text here ....")

    RESULT:
    -------
    inputEle3.OuterHtml => <input id="txt01" name="txt01" type="text" placeholder="text here ...." >

It works now, provided I use HtmlAgilityPack.dll :( Microsoft mshtml sucks! :(

No correct solution

OTHER TIPS

This is what worked for me. Forgive me using dynamic datatype, I do not have the mshtml library on my Visual Studio for some reason.

private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate("about:blank");
            this.webBrowser1.Document.Write("<INPUT id='hell' class='blah' placeholder='text here'   name='hell' type='text'></INPUT>");
            dynamic htmldoc = webBrowser1.Document.DomDocument as dynamic;
            dynamic node = htmldoc.getElementById("hell") as dynamic;
            string x = node.OuterHtml; //gets name but not type
            string s = node.GetAttribute["type"]; //gets type
            string name = node.GetAttribute["name"]; //gets name
        }

So the OuterHtml per say did not get the attribute, but when calling the GetAttribute method it did work. Hopefuly this helps.

ULTIMATE SOLUTION:

Use HTML Agility Pack:
----------------------
Dim inputEle3 As HtmlAgilityPack.HtmlNode = new_wb.CreateElement("input")
inputEle3.Attributes.Add("id", "txt01")
inputEle3.Attributes.Add("name", inputEle3.Id)
inputEle3.Attributes.Add("type", "text")
inputEle3.Attributes.Add("placeholder", "text here ....")

RESULT:
-------
inputEle3.OuterHtml => <input id="txt01" name="txt01" type="text" placeholder="text here ...." >

It works now, provided I use HtmlAgilityPack.dll :( Microsoft mshtml sucks! :(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top