Solved

Parse a string (very urgent)

Posted on 2004-09-30
2
258 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path of Workbook 3 59
silent install of security banner via msiexec command 4 74
Not needed 13 93
Full Screen problem when auto scale IFRAME 2 60
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now