Solved

Parse a string (very urgent)

Posted on 2004-09-30
2
252 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hasOne  challenge 59 88
endX challenge 2 48
firstChar challenge 13 83
c++ reading data from file into two dimensional array 3 23
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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

11 Experts available now in Live!

Get 1:1 Help Now