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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.


You can use getElementsByTagName("DIV") then you can iterate through the collection of objects.
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?
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?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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.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("" & strSymb & "&mkttype=after"),  _
        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)

Open in new window

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

Open in new window

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 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!
OK. You could filter for specific DIV in the iteration or if you only want a specific element use GetElementById().
For Each ....
If = "content_main_qn" then
 TextBox1.Text = element.innertext
 Exit For
End if

' OR

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

Open in new window

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.
investalertAuthor Commented:
Expert provided a direct simple solution that can be expanded to address other queries to web site. Great member of Experts Exchage
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.