470,614 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

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='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.

Sep 16 '08 #1
2 6376
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='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?
Sep 17 '08 #2
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='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?- 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
Sep 17 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by mike | last post: by
4 posts views Thread by blueey | last post: by
27 posts views Thread by Nicholas Couch | last post: by
4 posts views Thread by Joseph | last post: by
reply views Thread by hzgt9b | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.