Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Parse a string (very urgent)

Posted on 2004-09-30
2
Medium Priority
?
266 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
[X]
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
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
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…

704 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