Reputation: 54504
I am going to generate XML file based on the data returned from SQL Server, but there are some special characters like 
and 
(there may be other characters like these), which will fail the XML.
Is there any way to escape them?
Thanks!
Upvotes: 3
Views: 5270
Reputation: 54504
Well, I just use the pattern matching stuff to replace those special characters manually. Match for '&#.+?;'
Upvotes: 0
Reputation: 536339
The control characters U+001C (file separator) and U+001F (unit separator) are not legal to include in an XML 1.0 document, whether verbatim or encoded using a &#...;
numeric character reference.
They are allowed in XML 1.1 documents only when included as a character reference. However, XML 1.1 is not nearly as widely accepted as 1.0, and you can't have U+0000 (null) even as a character reference, so it's still not possible to put arbitrary binary data in an XML file — not that it was ever a good idea.
If you want to include data bytes in an XML file you should generally be using an ad hoc encoding of your own that is accepted by all consumers of your particular type of document. It is common to use base64 for the purpose of putting binary data into XML. For formats that do not accommodate any such special encoding scheme, you simply cannot insert these control characters.
What is the purpose of the control characters?
Upvotes: 5
Reputation: 48369
The exact same way you're escaping any other user-supplied input prior to insertion into a database; probably one of (from worst to best):
Upvotes: 1
Reputation: 65116
Use parametrized queries and you won't have to worry about escaping. Can't really give you more help as to how to use them unless you mention which language you're using.
Upvotes: 0