Ted
Ted

Reputation: 1760

Encode XML string

I have some text which I should save in to xml file. But inside text are some XML not allowed symbols (like unicode: 0xb). How I can filter them? Is in VBScript method similar to HTMLEncode.

Upvotes: 0

Views: 623

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

ASP has a HTMLEncode method, but plain VBScript does not. You need to implement it yourself, for instance like this:

Function EncodeCharacter(m, p, s)
    EncodeCharacter = "&#" & Asc(m) & ";"
End Function

Function EncodeString(s)
    Set re = New RegExp
    re.Pattern = "[^a-zA-Z0-9 .:;!?/_-]"
    re.Global  = True

    EncodeString = re.Replace(s, GetRef("EncodeCharacter"))
End Function

str = ...
encoded = EncodeString(str)

The above code uses a regular expression with a replacement function to substitute special characters with their corresponding numeric entity. Adjust the regular expression as you see fit, so that it covers all characters you don't want replaced.

Upvotes: 1

Related Questions