会社の既存のツールとのカスタム統合を使用して、会社で使用するための WYSIWYG アプリケーションを開発しています。
「.OuterHtml」を使用して html 文字列を取得しようとすると、特定の要素、特に INPUT タグ要素から「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を使用していれば、今は動作します:( Microsoft mshtmlは最悪です! :(