Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Parse a string (very urgent)

Posted on 2004-09-30
2
261 Views
Last Modified: 2010-04-17
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
Comment
Question by:sony1464
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 12195787
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
 
LVL 19

Expert Comment

by:drichards
ID: 12195864
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question