What characters must be escaped in XML documents, or where could I find such a list?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you use an appropriate class or library, they will do the escaping for you. Many XML issues are caused by string concatenation.
XML escape characters
There are only five:
Escaping characters depends on where the special character is used.
The examples can be validated at the W3C Markup Validation Service.
Text
The safe way is to escape all five characters in text. However, the three characters
",'and>needn’t be escaped in text:Attributes
The safe way is to escape all five characters in attributes. However, the
>character needn’t be escaped in attributes:The
'character needn’t be escaped in attributes if the quotes are":Likewise, the
"needn’t be escaped in attributes if the quotes are':Comments
All five special characters must not be escaped in comments:
CDATA
All five special characters must not be escaped in CDATA sections:
Processing instructions
All five special characters must not be escaped in XML processing instructions:
XML vs. HTML
HTML has its own set of escape codes which cover a lot more characters.