Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What characters do I need to escape in XML documents?

Which characters should be gotten away in XML records, or where could I discover such a list?
by

2 Answers

sandhya6gczb
There are only five:

" "
' '
< &lt;
> &gt;
& &amp;

Escaping characters depends on where the special character is used.
For Text

The safe way is to escape all five characters in text. However, the three characters ", ' and > needn't be escaped in text:

<?xml version="1.0"?>
<valid>"'></valid>

Attributes
The safe way is to escape all five characters in attributes. However, the > character needn't be escaped in attributes:

<?xml version="1.0"?>
<valid attribute=">"/>

The ' character needn't be escaped in attributes if the quotes are ":

<?xml version="1.0"?>
<valid attribute="'"/>
MounikaDasa
Escaping characters is different for tags and attributes.

For tags:

< &lt;
> &gt; (only for compatibility, read below)
& &amp;
For attributes:

" &quot;
' &apos;

Login / Signup to Answer the Question.