What are invalid characters in XML

xml

Solution

The only illegal characters are `&`, `<` and `>` (as well as `"` or `'` in attributes, depending on which character is used to delimit the attribute value: `attr="must use &quot; here, ' is allowed"` and `attr='must use &apos; here, " is allowed'`).

They're escaped using XML entities, in this case you want `&amp;` for `&`.

Really, though, you should use a tool or library that writes XML for you and abstracts this kind of thing away for you so you don't have to worry about it.

Problem

I am working with some XML that holds strings like: ``` <node>This is a string</node> ``` Some of the strings that I am passing to the nodes will have characters like `&`, `#`, `$`, etc.: ``` <node>This is a string & so is this</node> ``` This is not valid due to `&`. I cannot wrap these strings in CDATA as they need to be as they are. I tried looking for a list of characters that cannot be put in XML nodes without being in a CDATA. Can someone point me in the direction of one or provide me with a list of illegal characters?

Original source

Related problems