Ensuring that text is safe for XML is always a good rule of thumb when your data is being generated by a group or by people who are not technical. The following code does exactly that. I will ensure that text is stripped and replaced with any offending characters.
This chuck of code is part of a class that I call the FormatString Class that can be downloaded here. But, here is the code by itself:
'+--------------------------------------------------------------------------------------------------
' XMLSafe
' Transformed a string to XML safe format.
'+--------------------------------------------------------------------------------------------------
Function XMLSafe(str)
Dim xmlList
xmlList = getXMLList()
If InStr(str, "é") > 0 Then str = Replace(str, "é", "é")
xmlSafe = ChangeCharacters(str, xmlList, 3)
End Function
'Support for the XMLSafe function. Creates a list of characters to use to make the xml equivalent switch.
Function getXMLList()
Dim s
s = s & "Á,193,C1,Á|"
s = s & "á,225,E1,á|"
s = s & "Â,194,C2,Â|"
s = s & "â,226,E2,â|"
s = s & "´,180,B4,´|"
s = s & "Æ,198,C6,Æ|"
s = s & "æ,230,E6,æ|"
s = s & "À,192,C0,À|"
s = s & "à,224,E0,à|"
s = s & "Å,197,C5,Å|"
s = s & "å,229,E5,å|"
s = s & "Ã,195,C3,Ã|"
s = s & "ã,227,E3,ã|"
s = s & "Ä,196,C4,Ä|"
s = s & "ä,228,E4,ä|"
s = s & "„,132,84,„|"
s = s & "¦,166,A6,¦|"
s = s & "•,149,95,•|"
s = s & "Ç,199,C7,Ç|"
s = s & "ç,231,E7,ç|"
s = s & "¸,184,B8,¸|"
s = s & "¢,162,A2,¢|"
s = s & "ˆ,136,88,ˆ|"
s = s & "©,169,A9,©|"
s = s & "¤,164,A4,¤|"
s = s & "†,134,86,†|"
s = s & "‡,135,87,‡|"
s = s & "°,176,B0,°|"
s = s & "÷,247,F7,÷|"
s = s & "É,201,C9,É|"
s = s & "é,233,E9,é|"
s = s & "Ê,202,CA,Ê|"
s = s & "ê,234,EA,ê|"
s = s & "È,200,C8,È|"
s = s & "è,232,E8,è|"
s = s & "Ð,208,D0,Ð|"
s = s & "ð,240,F0,ð|"
s = s & "Ë,203,CB,Ë|"
s = s & "ë,235,EB,ë|"
s = s & "€,128,80,€|"
s = s & "ƒ,131,83,ƒ|"
s = s & "½,189,BD,½|"
s = s & "¼,188,BC,¼|"
s = s & "¾,190,BE,¾|"
s = s & "…,133,85,…|"
s = s & "Í,205,CD,Í|"
s = s & "í,237,ED,í|"
s = s & "Î,206,CE,Î|"
s = s & "î,238,EE,î|"
s = s & "¡,161,A1,¡|"
s = s & "Ì,204,CC,Ì|"
s = s & "ì,236,EC,ì|"
s = s & "¿,191,BF,¿|"
s = s & "Ï,207,CF,Ï|"
s = s & "ï,239,EF,ï|"
s = s & "«,171,AB,«|"
s = s & "“,147,93,“|"
s = s & "‹,139,8B,‹|"
s = s & "‘,145,91,‘|"
s = s & "¯,175,AF,¯|"
s = s & "—,45,97,—|"
s = s & "µ,181,B5,µ|"
s = s & "·,183,B7,•|"
s = s & " ,160,A0, |"
s = s & "–,45,96,–|"
s = s & "¬,172,AC,¬|"
s = s & "Ñ,209,D1,Ñ|"
s = s & "ñ,241,F1,ñ|"
s = s & "Ó,211,D3,Ó|"
s = s & "ó,243,F3,ó|"
s = s & "Ô,212,D4,Ô|"
s = s & "ô,244,F4,ô|"
s = s & "Œ,140,8C,Œ|"
s = s & "œ,156,9C,œ|"
s = s & "Ò,210,D2,Ò|"
s = s & "ò,242,F2,ò|"
s = s & "ª,170,AA,ª|"
s = s & "º,186,BA,º|"
s = s & "Ø,216,D8,Ø|"
s = s & "ø,248,F8,ø|"
s = s & "Õ,213,D5,Õ|"
s = s & "õ,245,F5,õ|"
s = s & "Ö,214,D6,Ö|"
s = s & "ö,246,F6,ö|"
s = s & "¶,182,B6,¶|"
s = s & "‰,137,89,‰|"
s = s & "±,177,B1,±|"
s = s & "£,163,A3,£|"
s = s & "»,187,BB,»|"
s = s & "”,148,94,”|"
s = s & "®,174,AE,®|"
s = s & "›,155,9B,›|"
s = s & "’,146,',’|"
s = s & "‚,130,82,‚|"
s = s & "Š,138,8A,Š|"
s = s & "š,154,9A,š|"
s = s & "§,167,A7,§|"
s = s & "­,173,AD,¬|"
s = s & "¹,185,B9,¹|"
s = s & "²,178,B2,²|"
s = s & "³,179,B3,³|"
s = s & "ß,223,DF,ß|"
s = s & "Þ,222,DE,Þ|"
s = s & "þ,254,FE,þ|"
s = s & "˜,152,98,˜|"
s = s & "×,215,D7,×|"
s = s & "™,153,99,™|"
s = s & "Ú,218,DA,Ú|"
s = s & "ú,250,FA,ú|"
s = s & "Û,219,DB,Û|"
s = s & "û,251,FB,û|"
s = s & "Ù,217,D9,Ù|"
s = s & "ù,249,F9,ù|"
s = s & "¨,168,A8,¨|"
s = s & "Ü,220,DC,Ü|"
s = s & "ü,252,FC,ü|"
s = s & "Ý,221,DD,Ý|"
s = s & "ý,253,FD,ý|"
s = s & "¥,165,A5,¥|"
s = s & "ÿ,159,9F,Ÿ|"
s = s & "ÿ,255,FF,ÿ"
s = s & "',',','"
s = s & "<ul>,,,"
s = s & "<li>, - , - , - "
s = s & "</li>,,,"
s = s & "</ul>,,,"
getXMLList = s
End Function
'Supports the XMLSafe function. This simply switches the characters from one to another.
Function ChangeCharacters(str, dict, intColumn)
Dim arr(), arrTemp, a
Dim n, i
arrTemp = Split(dict, "|")
Redim arr(2, UBound(arrTemp))
For n = 0 To UBound(arrTemp) -1
a = Split(arrTemp(n), ",")
If str <> "" Then
If InStr(str, a(0)) > 0 Then
str = Replace(str, a(0), Chr(a(1)))
End If
End If
Next
ChangeCharacters = str
End Function