Silverlight Tip of the Day #59 – How to Display Special Characters in XAML
If you try to use the following characters in a string in XAML you will get a slew of errors in your Error List:
- <
- >
- &
- “
For example, if you tried to do this:
<Button Width="100" Height="100" Content="Click &Me"></Button>
You would get these errors:
Error 1 '"' is an unexpected token. The expected token is ';'. Line 32, position 60.
Error 3 Entity references or sequences beginning with an ampersand '&' must be terminated with a semicolon ';'.
You can encode invalid characters for use in XAML by using the following syntax:
| Character | Encoding |
| < | < |
| > | > |
| & | & |
| “ | " |
In addition, the following character encoding is also useful:
| Character | Encoding |
| Space |   |
So to fix the Button above you would insert “&” wherever you want to use an “&”
<Button Width="100" Height="100" Content="Click &Me"></Button>
Thank you,
--Mike Snow
Subscribe in a reader