我有以下代码作为下拉选择事件,
'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
我刚刚发现了一个错误,具体取决于来自Web服务的XML响应类型。以下是回复示例
<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
当响应如上所述代码有效时 因为我已经附加了格式化程序,但是当响应完全关闭时,如下所示
<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">
我收到以下错误:
名称不能以
'"'
字符开头,十六进制值0x22。第1行,第2078位。描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。
异常详细信息:
System.Xml.XmlException
:名称不能以'"'
字符开头,十六进制值0x22。第1行,第2078位。来源错误:
Line 83: formater.Append(""" />") Line 84: 'MsgBox(formater.ToString) Line 85: Dim xmlDoc = XElement.Parse(formater.ToString) Line 86: Dim root As IEnumerable(Of XElement) = xmlDoc.Elements() Line 87: Dim names As StringBuilder
在我可以附加结尾部分或捕获异常并继续之前,请先了解如何检查XML响应。
答案 0 :(得分:0)
嗨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)
但是当我得到
时我的代码仍然会中断<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
例如,你知道我如何将银行名称作为下拉列表中的文本和代码作为值,因为它们在不同的下拉列表中显示它们彼此不匹配