views:

2367

answers:

3

Hey all,

I am working with some XML at the moment.

I have nodes that hold strings like below:

<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 the &

I cannot wrap these strings in CDATA as they need to be as they are. I have tried looking online for a list of characters that cannot be put in XML nodes without being in a CDATA.

Could anyone point me in the direction of one or provide me with a list of illegal characters?

Cheers

Eef

+1  A: 

The predeclared characters are:

& < > " '

http://xml.silmaril.ie/authors/specials/

altCognito
Ack...beat me to it.
Justin Niessner
+1  A: 

Check this link:

The XML FAQ: What are the special characters in XML?

Justin Niessner
+4  A: 

The only illegal characters are &, < and > (as well as " or ' in attributes).

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.

Welbog
[And ‘>’ doesn't always *have* to be escaped, either, although it's probably easiest to do so. It's only the string ‘]]>’ that's invalid (in element content). Bit of a strange wart really.]
bobince