Reputation: 4662
I am having a hard time parsing some simple xml data being sent back by a cc terminal.
Here is the data I'm given back:
"<PLCardPresent>0</PLCardPresent><PLEntryMode>1</PLEntryMode><PLNameOnCard>FRANKINSON/FRANK </PLNameOnCard><AmountDue>0</AmountDue><TipAmount>0</TipAmount><CashBackAmout>0</CashBackAmout><MerchantFee>0</MerchantFee><TaxAmount>0</TaxAmount><ExpDate>1219</ExpDate><ECRRefNum>666</ECRRefNum>"
I am attempting to parse it with this:
Dim myXmlDoc As New XmlDocument
myXmlDoc.Load(r.ExtData)
Dim ExpDate As String = ReturnXmlValue(myXmlDoc, "ExpDate")
Dim NameOnCard As String = ReturnXmlValue(myXmlDoc, "PLNameOnCard")
My method:
Protected Function ReturnXmlValue(ByVal myXDoc As Xml.XmlDocument, ByVal field As String) As String
Dim retval As String = String.Empty
Try
Dim node As Xml.XmlNodeList = myXDoc.GetElementsByTagName(field)
If node IsNot Nothing And node.Count > 0 Then
retval = node.Item(0).InnerText
End If
Catch ex As Exception
WriteException(ex)
Throw
End Try
Return retval
End Function
The errors is happening at the load of the xml doc.
Am I not parsing this the right way?
ADDITIONAL INFO
Since it was suggested that I check for null terminators, I made the following change, which I hope is what was meant:
Dim test As String = r.ExtData.Replace(ControlChars.NullChar, String.Empty)
Dim myXmlDoc As New XmlDocument
myXmlDoc.Load(test)
I still received the error I mention in the title.
As for what is the r.ExtData, it is the line of data that starts with that I highlighted above.
Dim r As PaymentResponse = posl.PaymentResponse
Which gives me something similiar to this:
Upvotes: 1
Views: 2094
Reputation: 59238
Your string is not valid XML.
XML needs to have one single root element. Yours has many XML elements on root level:
<PLCardPresent>0</PLCardPresent>
<PLEntryMode>1</PLEntryMode>
<PLNameOnCard>FRANKINSON/FRANK </PLNameOnCard>
<AmountDue>0</AmountDue>
<TipAmount>0</TipAmount>
<CashBackAmout>0</CashBackAmout>
<MerchantFee>0</MerchantFee>
<TaxAmount>0</TaxAmount>
<ExpDate>1219</ExpDate>
<ECRRefNum>666</ECRRefNum>
You can workaround this by adding a start root element at the beginning and a closing root element at the end so that it looks like
<root>
<PLCardPresent>0</PLCardPresent>
<PLEntryMode>1</PLEntryMode>
<PLNameOnCard>FRANKINSON/FRANK </PLNameOnCard>
<AmountDue>0</AmountDue>
<TipAmount>0</TipAmount>
<CashBackAmout>0</CashBackAmout>
<MerchantFee>0</MerchantFee>
<TaxAmount>0</TaxAmount>
<ExpDate>1219</ExpDate>
<ECRRefNum>666</ECRRefNum>
</root>
Second, note the major difference between the two methods
xml.Load(filename); // Filename as string
xml.LoadXml(xmlcontent); // XML as string
Upvotes: 3