簡體   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