如何在IE9中使用mshtml获得具有名称空间前缀的属性
本文关键字:有名称 空间 前缀 属性 mshtml IE9 | 更新日期: 2023-09-27 18:07:03
我们有一个用c#编写的浏览器帮助对象(BHO),它在IE8中工作得很好。但是,在IE9中访问名称空间中的标签和属性不再有效。例如,使用
<p xmlns:acme="http://www.acme.com/2007/acme">
<input type="text" id="input1" value="" acme:initial="initial"/>
</p>
下面的工作在IE8:
IHTMLElement element = doc.getElementById("input1");
String initial = element.getAttribute("evsp:initial", 0) as String;
IE8将"acme:initial"视为一个文本令牌,而IE9尝试使用"acme"作为命名空间前缀来识别更多的命名空间。
使用getAttributeNS似乎是合适的,但它似乎不起作用:
IHTMLElement6 element6 = (IHTMLElement6)element;
String initial6 = (String)element6.getAttributeNS("http://www.acme.com/2007/acme",
"initial");
在上面的例子中,element6被设置为一个mshtml。htmlputelementclass,但initial6为null
由于旧的文本标记和名称空间方法都不起作用,看起来我们被卡住了。
如果包含带有名称空间前缀的属性,那么迭代元素的实际属性也是可以的。
是否有一种方法与IE9安装,以获得名称空间前缀属性的值?
一些细节:Microsoft.mshtml.dll的默认PIA是版本7。IE9使用mshtml.dll版本9。我们使用c:' c:' Windows'System32'mshtml。tlb(与IE9一起安装)生成缺少的接口,如IHTMLElement6,并将它们包含在我们的项目中。我们过去已经成功地将此技术用于其他IE(N-1), IE(N)差异。
这是一种暴力破解方法,迭代所有属性:
// find your input element
IHTMLElement element = Doc3.getElementById("input1");
// get a collection of all attributes
IHTMLAttributeCollection attributes = (IHTMLAttributeCollection)((IHTMLDOMNode)Element).attributes;
// iterate all attributes
for (integer i = 0; i < attributes.length; i++)
{
IDispatch attribute = attributes.item(i);
// this eventually lists your attribute
System.Diagnostics.Debug.Writeln( ((IHTMLDOMAttribute) attribute).nodeName );
}
(抱歉语法错误,这是我的想法。)
这将您的输入元素视为原始DOM节点并迭代其属性。缺点是:您可以获得所有属性,而不仅仅是HTML中看到的属性。
更简单
IHTMLElementCollection InputCollection = Doc3.getElementsByTagName("input1");
foreach (IHTMLInputElement InputTag in InputCollection) { Console.WriteLine(InputTag.name); }