traverse through a string

I grab the following HTML from my page.

now I want a function which returns me all 'values' of a certain 'class'

e.g. If I give the function this HTML and a class name, say 'nenMeterRegister' then it should find the corresponding 'value' in same row , do that for every element which has a class 'nenMeterRegister' and returns an array of those elements.
how do I do this?
<table>
<tr class="nenDataRow">
                        <td>
                            <input type="checkbox" class="nenMakeInvalid" />
                        </td>
                        <td>
                            <input type="text"  class="nenMeterRegister" disabled="disabled" value="643234-1" />
                        </td>
                        <td>
                            <input type="text" class="nenNmiSuffix" disabled="disabled" value="11" />
                        </td>
                        
                    </tr>
<tr class="nenDataRow">
                        <td>
                            <input type="checkbox" class="nenMakeInvalid" />
                        </td>
                        <td>
                            <input type="text"  class="nenMeterRegister" disabled="disabled" value="643234-1" />
                        </td>
                        <td>
                            <input type="text" class="nenNmiSuffix" disabled="disabled" value="11" />
                        </td>
                        
                    </tr>
</table>

Open in new window

manivineetAsked:
Who is Participating?
 
ddayx10Connect With a Mentor Commented:
There are a million ways to parse strings, so this is just one approach, but it works fine with the sample you provided :)

    Public Function GetValuesFromHtmlString(ByVal html As String, ByVal className As String) As String()
        Dim rValue As New List(Of String)
        Dim x As String() = html.Split(New [Char]() {CChar(vbCrLf)})
        Dim pattern As String = "(\S)"

        For Each s As String In x
            If Not String.IsNullOrEmpty(s.Trim) And Regex.IsMatch(s, pattern) Then
                If s.Trim.Contains("class=" & ControlChars.Quote & className) And s.Trim.Contains("value=") Then
                    Dim sIdx As Integer = s.Trim.IndexOf("value=") + 7
                    Dim eIdx As Integer = s.Trim.IndexOf(ControlChars.Quote, sIdx)
                    rValue.Add(s.Trim.Substring(sIdx, eIdx - sIdx))
                End If
            End If
        Next
        Return rValue.ToArray()
    End Function
0
 
Alfred A.Commented:
Check this out:

http://www.quirksmode.org/dom/intro.html

for getting the value of an attribute:

for example:

txt=xmlDoc.getElementsByTagName("input")[0].getAttribute("nenMeterRegister");

see below for details:

http://www.w3schools.com/dom/dom_nodes_get.asp

I hope this helps.
0
 
manivineetAuthor Commented:
@Alfred1 : Thanks, but i need to do this with VB.Net
so in essence i need to get the values, as if this were a normal string and not HTML data.
0
 
Alfred A.Commented:
Try this.  It is not complete but I just provided some idea on how to get it.  I hope it helps.
Public Sub GetNenMeterRegisterValues()        
        Dim objStreamReader As New StreamReader("C:\traverse.txt")
        Dim strText As String = objStreamReader.ReadToEnd
        Dim strLength As Integer = strText.Length
        Dim Pos As Integer = 1
        Dim SecondPos As Integer = 0
        Dim nenMeterRegisterValues As String
        Do
            Pos = InStr(Pos, strText, "class=""nenMeterRegister""")
            If Pos > 0 Then
                SecondPos = InStr(Pos, strText, "value=""")
                If SecondPos > 0 Then
                    'Get Value
                    nenMeterRegisterValues = nenMeterRegisterValues & (strText, SecondPos + 7, 8) & ","
                    Pos = SecondPos
                End If
            Else
                Exit Do
            End If
        Loop
End Sub

Open in new window

0
 
Alfred A.Connect With a Mentor Commented:
Oops.  Correction on the code I provided.  I am posting it again.  Mid is missing in line 14.
Public Sub GetNenMeterRegisterValues() 
        Dim objStreamReader As New StreamReader("C:\traverse.txt")
        Dim strText As String = objStreamReader.ReadToEnd
        Dim strLength As Integer = strText.Length
        Dim Pos As Integer = 1
        Dim SecondPos As Integer = 0
        Dim nenMeterRegisterValues As String = String.Empty
        Do
            Pos = InStr(Pos, strText, "class=""nenMeterRegister""")
            If Pos > 0 Then
                SecondPos = InStr(Pos, strText, "value=""")
                If SecondPos > 0 Then
                    ''Get Value
                    nenMeterRegisterValues = nenMeterRegisterValues & Mid(strText, SecondPos + 7, 8) & ","
                    Pos = SecondPos
                End If
            Else
                Exit Do
            End If
        Loop
End Sub

Open in new window

0
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.

All Courses

From novice to tech pro — start learning today.