Help: How can I enumerate span element attributes using MSHTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cloftis

    #1

    Help: How can I enumerate span element attributes using MSHTML

    Using VS2003, VB and MSHTML,
    Using an HTMLSpanElement I want to enumerate the attributes of a SPAN
    tag.

    1 'For testing sake
    2 Dim strMarkup as String = "<span attr1='somevalu e'
    attr2='somevalu e' attrN='...'>mar kup</span>"
    3 Dim objSpan As HTMLSpanElement =
    browser.Documen t.createElement ("span")
    4 objSpan.innerHT ML = strMarkup

    After the above code runs, I want to loop throught all the attributes
    and inspect their names and values but I haven't found an easy way to
    accomplish this... any suggestions?

    I've tried using objSpan.attribu tes but it always has a large number
    of elements in the array regardless of the number of elements I put in
    strMarkup.

    Your thoughts appreciated.



  • cloftis

    #2
    Re: Help: How can I enumerate span element attributes using MSHTML

    On Sep 16, 5:07 pm, cloftis <celof...@gmail .comwrote:
    Using VS2003, VB and MSHTML,
    Using an HTMLSpanElement I want to enumerate the attributes of a SPAN
    tag.
    >
    1     'For testing sake
    2     Dim strMarkup as String = "<span attr1='somevalu e'
    attr2='somevalu e' attrN='...'>mar kup</span>"
    3     Dim objSpan As HTMLSpanElement =
    browser.Documen t.createElement ("span")
    4     objSpan.innerHT ML = strMarkup
    >
    After the above code runs, I want to loop throught all the attributes
    and inspect their names and values but I haven't found an easy way to
    accomplish this...  any suggestions?
    >
    I've tried using objSpan.attribu tes but it always has a large number
    of elements in the array regardless of the number of elements I put in
    strMarkup.
    >
    Your thoughts appreciated.
    Some progress... here's the code that I came up with to enumerate an
    element's attributes:

    Dim iElement As IHTMLElement =
    browser.Documen t.createElement ("span")
    iElement.innerH TML = "<span attr1='somevalu e'
    attr2='somevalu e' attrN='...'>mar kup</span>"
    Dim iNode As IHTMLDOMNode = CType(iElement, IHTMLDOMNode)
    Dim iAttrColl As IHTMLAttributeC ollection =
    CType(iNode.att ributes, IHTMLAttributeC ollection)
    Try
    For Each attr As IHTMLDOMAttribu te2 In iAttrColl
    If CType(attr, IHTMLDOMAttribu te).specified Then
    Debug.WriteLine (attr.name & ":" & attr.value)
    End If
    Next
    Catch ex As Exception
    Debug.WriteLine (ex.ToString)
    End Try

    The problem I am still having is that the innerHTML that I specify
    does not appear to get parsed so when I loop throught the list of
    attributes my custom attributes are not found...

    How can I get enumerate the attributes specificed in the innerHTML of
    the span tag above?

    Comment

    • cloftis

      #3
      Re: Help: How can I enumerate span element attributes using MSHTML

      On Sep 17, 9:21 am, cloftis <celof...@gmail .comwrote:
      On Sep 16, 5:07 pm, cloftis <celof...@gmail .comwrote:
      >
      >
      >
      >
      >
      Using VS2003, VB and MSHTML,
      Using an HTMLSpanElement I want to enumerate the attributes of a SPAN
      tag.
      >
      1     'For testing sake
      2     Dim strMarkup as String = "<span attr1='somevalu e'
      attr2='somevalu e' attrN='...'>mar kup</span>"
      3     Dim objSpan As HTMLSpanElement =
      browser.Documen t.createElement ("span")
      4     objSpan.innerHT ML = strMarkup
      >
      After the above code runs, I want to loop throught all the attributes
      and inspect their names and values but I haven't found an easy way to
      accomplish this...  any suggestions?
      >
      I've tried using objSpan.attribu tes but it always has a large number
      of elements in the array regardless of the number of elements I put in
      strMarkup.
      >
      Your thoughts appreciated.
      >
      Some progress... here's the code that I came up with to enumerate an
      element's attributes:
      >
                  Dim iElement As IHTMLElement =
      browser.Documen t.createElement ("span")
                  iElement.innerH TML = "<span attr1='somevalu e'
      attr2='somevalu e' attrN='...'>mar kup</span>"
                  Dim iNode As IHTMLDOMNode = CType(iElement, IHTMLDOMNode)
                  Dim iAttrColl As IHTMLAttributeC ollection =
      CType(iNode.att ributes, IHTMLAttributeC ollection)
                  Try
                      For Each attr As IHTMLDOMAttribu te2 In iAttrColl
                          If CType(attr, IHTMLDOMAttribu te)..specified Then
                              Debug.WriteLine (attr.name& ":" & attr.value)
                          End If
                      Next
                  Catch ex As Exception
                      Debug.WriteLine (ex.ToString)
                  End Try
      >
      The problem I am still having is that the innerHTML that I specify
      does not appear to get parsed so when I loop throught the list of
      attributes my custom attributes are not found...
      >
      How can I get enumerate the attributes specificed in the innerHTML of
      the span tag above?- Hide quoted text -
      >
      - Show quoted text -
      Ok, I finally licked this. Instead of using the MSHTML.dll, I used XML
      objects instread. Here's what I ended up with:

      Dim xDoc As New Xml.XmlDocument
      xDoc.LoadXml("< span attr1='somevalu e'
      attr2='somevalu e' attrN='...'>mar kup</span>")
      If xDoc.ChildNodes (0).Attributes. Count 0 Then
      For Each xAttribute As Xml.XmlAttribut e In
      xDoc.ChildNodes (0).Attributes
      Debug.WriteLine (xAttribute.Nam e & ":" &
      xAttribute.Valu e)
      Next
      End If

      Maybe this will be able to help someone else.

      Tags: MSHTML, XML, Element attributes, span attributes, DOM Element
      attributes

      Comment

      Working...