XML String Created with VBA has Random Line Breaks

tommyboy115
tommyboy115 used Ask the Experts™
on
Hello,

I'm creating a fairly long XML string with VBA and it's inserting random line breaks, which are breaking the XML if they end up in the middle of a tag.  One reason the strings are so long is that I'm converting a PDF to Base64 as part of it.  I've tried using a replace function to remove the Chr$(13) and Chr$(10) characters, but that doesn't seem to be doing it.

Here's a shorter example, but with the same problem at the end:

<?xml version="1.0"?><OutboundRequest><AccessControl><UserName>#####</UserName><Password>####</Password></AccessControl><Transmission><TransmissionControl><TransmissionID>TMH</TransmissionID><Resolution>STANDARD</Resolution><Priority>NORMAL</Priority><SelfBusy>ENABLE</SelfBusy><FaxHeader>@DATE1   @TIME3   ANYWHERE (555) 124-1215   Page%P of @TPAGES</FaxHeader></TransmissionControl><DispositionControl><DispositionURL></DispositionURL><DispositionLevel>BOTH</DispositionLevel><DispositionMethod>EMAIL</DispositionMethod><DispositionEmails><DispositionEmail><DispositionRecipient>JANE DOE</DispositionRecipient><DispositionAddress>JANEDOE@GOOGLE.COM</DispositionAddress></DispositionEmail></DispositionEmails></DispositionControl><Recipients><Recipient><RecipientName>JANE DOE</RecipientName><RecipientCompany></RecipientCompany><RecipientFax>0000000000</RecipientFax></Recipient></Recipients><Files><File><FileContents></FileContents><FileType>pdf</FileType></File></Files></Transmission></OutboundReq
uest>

The last tag is broken and that causes the subsequent HTTP post to fail.  When looking at it in Notepad ++, it's a CRLF in there.  I'm not sure why I can't remove it.

There are the functions I'm using to remove them, with no luck:

Function RemoveReturns(varName As Variant) As Variant

    If (IsNull(varName)) Then
       
        Exit Function
   
    Else
   
    Dim i As Integer, varHold As Variant
   
      For i = 1 To Trim(Len(varName))
        If Mid(varName, i, 1) <> Chr$(13) Then
         varHold = varHold & Mid(varName, i, 1)
         Else
         varHold = varHold & "  "
        End If
     Next i
             
    RemoveReturns = varHold
   
    End If
   
End Function

Function RemoveLFs(varName As Variant) As Variant

    If (IsNull(varName)) Then
       
        Exit Function
   
    Else
   
    Dim i As Integer, varHold As Variant
   
      For i = 1 To Trim(Len(varName))
        If Mid(varName, i, 1) <> Chr$(10) Then
         varHold = varHold & Mid(varName, i, 1)
         Else
         varHold = varHold & "  "
        End If
     Next i
             
    RemoveLFs = varHold
   
    End If
   
End Function

Any help is appreciated!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
what if you replace CRLF directly like this?

Function RemoveCRLFs(varName As Variant) As Variant
    RemoveCRLFs = Replace(varName, vbCrLf, "")
End Function

Open in new window

Author

Commented:
That doesn't get rid of it either.
Ryan ChongSoftware Team Lead

Commented:
what if changing:
For i = 1 To Trim(Len(varName))

Open in new window


to

For i = 1 To Len(varName)

Open in new window


?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Ryan ChongSoftware Team Lead

Commented:
or try something like this:

Function RemoveCRLFs(varName As Variant) As Variant
    RemoveCRLFs = Replace(Replace(Replace(varName, vbCrLf, ""), Chr(10), ""), Chr(13), "")
End Function

Open in new window

I ended up just adding line breaks where I DID want them, like right after each tag.  That didn't eliminate the random ones, but made it so they showed up in other spots that didn't matter.
Ryan ChongSoftware Team Lead

Commented:
well, you got to debug your codes so that you know what are the "random ones"...

if this question is no longer important, pls go ahead and delete the question.

Author

Commented:
I figured out a solution that worked for me since none of the other suggestions worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial