Help: How can I enumerate span element attributes using MSHTML

C

cloftis

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='somevalue'
attr2='somevalue' attrN='...'>markup</span>"
3 Dim objSpan As HTMLSpanElement =
browser.Document.createElement("span")
4 objSpan.innerHTML = 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.attributes 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.
 
C

cloftis

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='somevalue'
attr2='somevalue' attrN='...'>markup</span>"
3     Dim objSpan As HTMLSpanElement =
browser.Document.createElement("span")
4     objSpan.innerHTML = 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.attributes 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.Document.createElement("span")
iElement.innerHTML = "<span attr1='somevalue'
attr2='somevalue' attrN='...'>markup</span>"
Dim iNode As IHTMLDOMNode = CType(iElement, IHTMLDOMNode)
Dim iAttrColl As IHTMLAttributeCollection =
CType(iNode.attributes, IHTMLAttributeCollection)
Try
For Each attr As IHTMLDOMAttribute2 In iAttrColl
If CType(attr, IHTMLDOMAttribute).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?
 
C

cloftis

Some progress... here's the code that I came up with to enumerate an
element's attributes:

            Dim iElement As IHTMLElement =
browser.Document.createElement("span")
            iElement.innerHTML = "<span attr1='somevalue'
attr2='somevalue' attrN='...'>markup</span>"
            Dim iNode As IHTMLDOMNode = CType(iElement, IHTMLDOMNode)
            Dim iAttrColl As IHTMLAttributeCollection =
CType(iNode.attributes, IHTMLAttributeCollection)
            Try
                For Each attr As IHTMLDOMAttribute2 In iAttrColl
                    If CType(attr, IHTMLDOMAttribute)..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='somevalue'
attr2='somevalue' attrN='...'>markup</span>")
If xDoc.ChildNodes(0).Attributes.Count > 0 Then
For Each xAttribute As Xml.XmlAttribute In
xDoc.ChildNodes(0).Attributes
Debug.WriteLine(xAttribute.Name & ":" &
xAttribute.Value)
Next
End If

Maybe this will be able to help someone else.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top