简体   繁体   中英

Formatting XML Response VB.NET

I have the following code that works as a dropdown select event,

'calling ersal webservice for banks
Dim service As New IMPALA.IMPALA
Dim bank As String
bank = service.Remittance_Bank(username, password, pin, code, txtbenfadd.Text)
'MsgBox(bank)
'formating the xml responce
Dim formater As StringBuilder
formater = New StringBuilder()
formater.Append("<?xml version=""1.0"" encoding=""UTF-8""?><root>")
formater.Append(bank)

formater.Append(""" /></root>")
'MsgBox(formater.ToString)
Dim xmlDoc = XElement.Parse(formater.ToString)
Dim root As IEnumerable(Of XElement) = xmlDoc.Elements()
Dim names As StringBuilder
Dim names1 As StringBuilder
names = New StringBuilder
names1 = New StringBuilder
For Each row In root
    Dim attr As XAttribute
    Dim attr2 As XAttribute
    For Each attr In root.Attributes("BANK_CODE").AsEnumerable

        names.Append(attr.Value + "|")
    Next
    For Each attr2 In root.Attributes("BANK_NAME").AsEnumerable

        names1.Append(attr2.Value + "|")
    Next
Next
Dim strArr() As String
Dim strArr1() As String
strArr = names.ToString.Split("|")
strArr1 = names1.ToString.Split("|")
Dim itemsList As ArrayList
itemsList = New ArrayList()
itemsList.Add(strArr)
itemsList.Add(strArr1)
'MsgBox(names.ToString)
For count = 0 To strArr1.Length - 1
    ddlbank.Items.Add(strArr1(count))
    ddlbnkcode.Items.Add(strArr(count))
Next

I just discovered a bug depending on the kind of XML response that comes from the web service. Below is a sample response

<row BANK_CODE="111" BANK_NAME="KENYAN BANK"/>
<row BANK_CODE="121" BANK_NAME="EAST BANK"/>
<row BANK_CODE="185" BANK_NAME="NAKUMATT BANK"/>
<row BANK_CODE="125" BANK_NAME="KENYAN CONSUMER BANK"/>
<row BANK_CODE="174

when the response is as above the code works since I have appended it with the formatter but when the response is well closed like below

<row BANK_CODE="111" BANK_NAME="KENYAN BANK"/>
<row BANK_CODE="121" BANK_NAME="EAST BANK"/>
<row BANK_CODE="185" BANK_NAME="NAKUMATT BANK"/>
<row BANK_CODE="125" BANK_NAME="KENYAN CONSUMER BANK"/>
<row BANK_CODE="174" BANK_NAME="TUSKYS BUYERS BANK">

I get the following error:

Name cannot begin with the '"' character, hexadecimal value 0x22. Line 1, position 2078.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException : Name cannot begin with the '"' character, hexadecimal value 0x22. Line 1, position 2078.

Source Error:

 Line 83: formater.Append(""" />")\nLine 84: 'MsgBox(formater.ToString)\nLine 85: Dim xmlDoc = XElement.Parse(formater.ToString)\nLine 86: Dim root As IEnumerable(Of XElement) = xmlDoc.Elements()\nLine 87: Dim names As StringBuilder

Any help please on how I can check the XML response before I can append the ending part or catch the exception and continue.

hi GSerg i do not have control of what the web-service returns since am integrating into someone else s' system i have come up with a way to catch some errors by edditing code like this

 Dim formater As StringBuilder
        formater = New StringBuilder()
        formater.Append("<?xml version=""1.0"" encoding=""UTF-8""?><root>")
        formater.Append(bank)
        Dim xmlDoc As XElement

        If bank.EndsWith("/>") Then
            Try
                formater.Append("</root>")
                xmlDoc = XElement.Parse(formater.ToString)
            Catch
                MsgBox("INVALID XML")
            End Try
        ElseIf bank.EndsWith("<row ") Then
            Try
                formater.Append(" /></root>")
                xmlDoc = XElement.Parse(formater.ToString)
            Catch
                MsgBox("INVALID XML")
            End Try
        ElseIf bank.EndsWith("<row") Then
            Try
                formater.Append(" /></root>")
                xmlDoc = XElement.Parse(formater.ToString)
            Catch
                MsgBox("INVALID XML")
            End Try
        Else
            Try
                formater.Append(""" /></root>")
                xmlDoc = XElement.Parse(formater.ToString)
            Catch
                MsgBox("INVALID XML")
            End Try
        End If
       'just displaying the fromatted xml to see the end result for test purposes
        MsgBox(formater.ToString)

though still my code breaks when i get

<row BANK_CODE="111" BANK_NAME="KENYAN BANK"/>
<row BANK_CODE="121" BANK_NAME="EAST BANK"/>
<row BANK_CODE="185" BANK_NAME="NAKUMATT BANK"/>
<row BANK_CODE="125" BANK_NAME="KENYAN CONSUMER BANK"/>
<row BANK_CODE

for example would you know how i can get the bank name as the text on the dropdown and the code as value since displaying them on different drop downs they do not match with each other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM