XML Encoding “European” characters

So while in Sweden one of the things I have been working on required me to output some XML from a view. I have a standard function which I use to encode text so that it will work correctly. But I learned two things which may well be blindingly obvious to you but were new to me…

Firstly, if you’re using a European character set which has lots of high number ASCII characters in then you want to be using ISO-8859-1, not UTF-8, as your XML encoding style:

<?xml version="1.0" encoding="ISO-8859-1" ?>

Second, where I had just encoded the basic 5 characters in the past: <, >, ", ‘ and &, you also need to translate any character with an ASCII code above 127 to it’s XML representation. So my new XMLEncode function looks like this:

Function XMLEncode (TextData As String) As String
    Dim MyChar As String
    Dim F_XML As String
    Dim i As Integer
    
    F_XML=""
    For i=1 To Len(TextData)
        MyChar=Mid(TextData,i,1)
        Select Case MyChar
        Case "<"
            F_XML= F_XML & "&lt;"
        Case ">"
            F_XML= F_XML & "&gt;"
        Case |"|
            F_XML= F_XML & "&quot;"
        Case "’"
            F_XML= F_XML & "’"
        Case "&"
            F_XML= F_XML & "&amp;"
        Case Else
            If Asc(myChar) > 127 Then
                F_XML = F_XML & "&#" + Cstr(Asc(myChar)) + ";"
            Else
                F_XML= F_XML & MyChar
            End If

        End Select
    Next
    XMLEncode=F_XML
End Function

PS: I have only changed the bold lines above, I don’t think I wrote the rest of the function but I can’t remember where i got it from so if you recognise it please let me know and I’ll add your credits!

Share