繁体   English   中英

VB.NET / C#.Net MSHTML:对某些元素使用“ setAttribute('name',value)”后,无法从Outerhtml获取“ name”属性

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

我正在开发一种所见即所得的应用程序,专门用于与公司现有工具进行自定义集成的公司用途。

当尝试通过使用“ .OuterHtml”(尤其是INPUT标签元素)获取html字符串时,无法从某些元素中获取“ name”属性。

代码示例:

    `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>"

是的,不仅缺少名称属性,还有一些其他属性。 (例如TYPE)有人可以帮助我吗?

尝试的解决方案:

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

**失败** :(

最终解决方案:

    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 ...." >

只要我使用HtmlAgilityPack.dll :(微软mshtml很烂!:(

这对我有用。 原谅我使用dynamic数据类型,由于某种原因我在Visual Studio上没有mshtml库。

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
        }

所以说的OuterHtml没有获得属性,但是在调用GetAttribute方法时它确实起作用。 希望这会有所帮助。

最终解决方案:

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 ...." >

只要我使用HtmlAgilityPack.dll :(微软mshtml很烂!:(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM