1

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:

enter image description here

4
  • What is the r.ExtData that you use to load the XML data? Commented Mar 17, 2015 at 19:25
  • I responded in the post to you two. Ty for your comments. Commented Mar 17, 2015 at 19:30
  • You're using the wrong method. Load accepts a filename, not an XML string. Commented Mar 17, 2015 at 19:32
  • use LoadXml to load from string Commented Mar 17, 2015 at 19:32

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

I did both changes you suggested and it worked perfectly. TYVM!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.