How do I escape ampersands in XML so they are rendered as entities in HTML?

Viewed 788801

I have some XML text that I wish to render in an HTML page. This text contains an ampersand, which I want to render in its entity representation: &.

How do I escape this ampersand in the source XML? I tried &, but this is decoded as the actual ampersand character (&), which is invalid in HTML.

So I want to escape it in such a way that it will be rendered as & in the web page that uses the XML output.

10 Answers

The & character is itself an escape character in XML so the solution is to concatenate it and a Unicode decimal equivalent for & thus ensuring that there are no XML parsing errors. That is, replace the character & with &.

Related