【问题标题】:How to get attributes with a namespace prefix in IE9 with mshtml?如何使用 mshtml 在 IE9 中获取具有命名空间前缀的属性?
【发布时间】:2011-08-28 11:15:29
【问题描述】:

我们有一个用 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.HTMLInputElementClass,但 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) 差异。

【问题讨论】:

    标签: c# internet-explorer-9 bho mshtml


    【解决方案1】:

    这是一种蛮力方法,迭代所有属性:

      // 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 中看到的那些。

    【讨论】:

      【解决方案2】:

      更简单

      IHTMLElementCollection InputCollection = Doc3.getElementsByTagName("input1");
      foreach (IHTMLInputElement InputTag in InputCollection) { Console.WriteLine(InputTag.name); }
      

      【讨论】:

        猜你喜欢
        • 2021-12-14
        • 2013-08-14
        • 1970-01-01
        • 2023-03-09
        • 2020-03-09
        • 1970-01-01
        • 2011-05-09
        • 2013-06-30
        相关资源
        最近更新 更多