• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Parse a string (very urgent)

Hi all,

           I have written a data entry with asp.net and vb.net. When the user finishes keying in and presses submit, i call a web service and pass the entries. The web service over which i have no control over returns the result as a string. This string contains the errors in the transaction. Now the string that is returned looks exactly like what is shown below. All i want from here is the Field Name and its corresponding error message as highlighted below. I just need the fields that have erros on them. Can somebody show me a code in vb.net to parse the string below and get the field and its error message. any help would be well appreciated


<Response>
    <Status Value= "Fail"/>
         <RuleSet>
         <Name Value="app1"/><Status Value="Errors Found"/>
            <Rules>
            <Rule>
                  <Name Value="Lookup"/>
                     <Errors>
                         <Error>
      ------>            <Field Name="personid"/>
      ------->          <ErrorMessage Value="personid has invalid value of 0"/>
                          </Error>
                           <Error>
      ------>             <Field Name="status id"/>
      ------>              <ErrorMessage Value="invalid staus id of 2"/>
                            </Error>
                           <Error>
      ----->                <Field Name="genderid"/>
      ------>              <ErrorMessage Value="invalid gender id of 0"/>
                           </Error>
                    </Errors>
               </Rule>
            </Rules>
       </RuleSet>
</Response>
0
sony1464
Asked:
sony1464
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one way to do it (you would just need to pass your string directly to parseString).  There is probably a way to do it using the XML classes but I am not familiar enough with them (or ASP.Net) to offer advice there.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' get the input string(I used a file)
        Dim fileName As String = Application.StartupPath & "\Input.txt"
        Dim sr As New System.IO.StreamReader(fileName)
        Dim inputString As String = sr.ReadToEnd
        sr.Close()

        parseString(inputString)
    End Sub

    Private Sub parseString(ByVal inputString As String)
        ' tracking variables
        Dim fieldNameTag As String = "<Field Name=" & Chr(34)
        Dim errorMessageTag As String = "<ErrorMessage Value=" & Chr(34)
        Dim endTag As String = Chr(34) & "/>"
        Dim fieldNamePos As Integer
        Dim errorMessagePos As Integer
        Dim endTagPos As Integer
        Dim valuePos As Integer
        Dim valueLength As Integer
        Dim fieldValue As String
        Dim errorValue As String
        Dim fields As New ArrayList
        Dim errors As New ArrayList

        ' parse the input string
        fieldNamePos = inputString.IndexOf(fieldNameTag)
        While fieldNamePos <> -1
            valuePos = fieldNamePos + fieldNameTag.Length
            endTagPos = inputString.IndexOf(endTag, valuePos)
            If endTagPos <> -1 Then
                valueLength = endTagPos - valuePos
                fieldValue = inputString.Substring(valuePos, valueLength)

                errorMessagePos = inputString.IndexOf(errorMessageTag, endTagPos + endTag.Length)
                If errorMessagePos <> -1 Then
                    valuePos = errorMessagePos + errorMessageTag.Length
                    endTagPos = inputString.IndexOf(endTag, valuePos)
                    If endTagPos <> -1 Then
                        valueLength = endTagPos - valuePos
                        errorValue = inputString.Substring(valuePos, valueLength)

                        fields.Add(fieldValue)
                        errors.Add(errorValue)

                        fieldNamePos = inputString.IndexOf(fieldNameTag, endTagPos)
                    Else
                        MsgBox("Missing Tag", MsgBoxStyle.Critical, "Error")
                        Exit While
                    End If
                Else
                    MsgBox("Missing Tag", MsgBoxStyle.Critical, "Error")
                    Exit While
                End If
            Else
                MsgBox("Missing Tag", MsgBoxStyle.Critical, "Error")
                Exit While
            End If
        End While

        ' enumerate the fields and their errors
        Dim i As Integer
        For i = 0 To fields.Count - 1
            Debug.WriteLine(fields(i) & " = " & errors(i))
        Next
    End Sub
0
 
drichardsCommented:
Here's a way to use XML parser, assuming you have loaded 'errorString with the XML string:

        Dim xmldoc As New System.Xml.XmlDocument
        xmldoc.LoadXml(errorString)
        Dim list As System.Xml.XmlNodeList = xmldoc.SelectNodes("//Error")
        For Each node As System.Xml.XmlNode In list
            Dim fieldName As String = node.ChildNodes(0).Attributes(0).InnerText
            Dim msg As String = node.ChildNodes(1).Attributes(0).InnerText
        Next

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now