简体   繁体   English

格式化 XML 响应 VB.NET

[英]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.我刚刚发现了一个错误,具体取决于来自 Web 服务的 XML 响应的类型。 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.名称不能以'"'字符开头,十六进制值 0x22。第 1 行,位置 2078。

Description: An unhandled exception occurred during the execution of the current web request.说明:在执行当前 Web 请求期间发生未处理的异常。 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.异常详细信息: System.Xml.XmlException :名称不能以'"'字符开头,十六进制值 0x22。第 1 行,位置 2078。

Source Error:源错误:

 Line 83: formater.Append(""" />")第 83 行:formater.Append(""" />")\nLine 84: 'MsgBox(formater.ToString)第 84 行:'MsgBox(formater.ToString)\nLine 85: Dim xmlDoc = XElement.Parse(formater.ToString)第 85 行:Dim xmlDoc = XElement.Parse(formater.ToString)\nLine 86: Dim root As IEnumerable(Of XElement) = xmlDoc.Elements()第 86 行:将根变暗为 IEnumerable(Of XElement) = xmlDoc.Elements()\nLine 87: Dim names As StringBuilder第 87 行:将名称变暗为 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.在我可以附加结束部分或捕获异常并继续之前,请帮助我如何检查 XML 响应。

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嗨,GSerg,我无法控制网络服务返回的内容,因为我正在集成到其他人的系统中,我想出了一种通过编辑这样的代码来捕获一些错误的方法

 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例如,您是否知道我如何将银行名称作为下拉列表中的文本和代码作为值,因为在不同的下拉列表中显示它们,它们彼此不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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