Narrow search to HTML element with known attribute ID

I have the following code that displays the innerText of all "div"s in URL. I want to add code to limit the innerText contained in the "div" with the ID attribute "content_main_qn" (i.e., ID="content_main_qn"). How do I modify the code to do this?
Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To Doc.all.length - 1
            Dim hElm As mshtml.IHTMLElement = _
            DirectCast(Doc.all.item(i), mshtml.IHTMLElement)
            Select Case hElm.tagName.ToLower
                'Case "body", "div", "table", "form"
                Case "div"
                Case Else
                    If hElm.innerText <> "" Then
                        sb.Append(hElm.innerText & vbCrLf)
                    End If
            End Select

Open in new window

investalertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MedievalWarriorCommented:
Hi,

You can use getElementsByTagName("DIV") then you can iterate through the collection of objects.
0
investalertAuthor Commented:
Thanks for your quick reply on a Saturday. I was thinking of using getelementsbyTagName or getelementsbyID. Where and what coding needs to be added to make either getElements... work?
0
MedievalWarriorCommented:
How are you using the code? It looks like your using mshtml so I assume you don't want to use the WebBrowser object? So are you using mshtml .NET import?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

investalertAuthor Commented:
Yes, you are quite observent! Please see the code below.
Dim TextBox3 As New TextBox()
        With TextBox3
            .Text() = "aig"
            .Location = New Point(70, 5)
            .Size = New Size(50, 10)
        End With
        Me.Controls.Add(TextBox3)

        Me.TextBox1.Multiline = True
        Me.TextBox1.ScrollBars = ScrollBars.Both
        Me.TextBox1.Size = New Size(900, 300)

        Dim strSymb As String
        strSymb = TextBox3.Text

        'above only for showing the sample
        Dim Doc As mshtml.IHTMLDocument
        Doc = New mshtml.HTMLDocumentClass
        Dim wbReq As Net.HttpWebRequest = _
            DirectCast(Net.WebRequest.Create("http://www.nasdaq.com/aspxcontent/ExtendedTradingTrades.aspx?selected=" & strSymb & "&mkttype=after"),  _
            Net.HttpWebRequest)
        Dim wbResp As Net.HttpWebResponse = _
            DirectCast(wbReq.GetResponse(), Net.HttpWebResponse)
        Dim wbHCol As Net.WebHeaderCollection = wbResp.Headers
        Dim myStream As IO.Stream = wbResp.GetResponseStream()
        Dim myreader As New IO.StreamReader(myStream)
        Doc.write(myreader.ReadToEnd())

Open in new window

0
MedievalWarriorCommented:
It should look something like this to to get the collection of DIV elements.
Dim elementCollection As IHTMLElementCollection = Doc.getElementsByTagName("DIV")

For Each element As IHTMLElement In elementCollection
 Debug.Print(element.id)
 Debug.Print(element.className)
Next

Open in new window

0
investalertAuthor Commented:
OK, I can run the code and get a listing of the IDs and ClassNames in the Immediate Window. Now I want the code in my question, at the beginning of my post, to show the innerText contained in the div with ID="content_main_qn" as the ONLY text displayed in TextBox1. The code now displays ALL innerText in all div's at the URL.

FYI, the code in my question comes after the code in my subsequent response. I am using vb.net 2010 and the current project runs without errors. I now want to narrow the text displayed in TextBox1 to that in the paragraph above. Thanks!
0
MedievalWarriorCommented:
OK. You could filter for specific DIV in the iteration or if you only want a specific element use GetElementById().
For Each ....
If element.id = "content_main_qn" then
 TextBox1.Text = element.innertext
 Exit For
End if
Next..


' OR

Dim element as IHtmlElement = Doc.getElementById("content_main_qn")
TextBox1.Text = element.innerText

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
investalertAuthor Commented:
Excellent! I was trying to make the solution more complicated than it should be. Thanks for your rapid response and solution.
0
investalertAuthor Commented:
Expert provided a direct simple solution that can be expanded to address other queries to web site. Great member of Experts Exchage
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.