1 0 Tag Archives: special characters
post icon

Silverlight Tip of the Day#27 – Displaying Special Characters in XAML

If you try to use the special/reserved characters in a string in XAML you will get a slew of errors in your Error List. Example reserved characters include:

  • <
  • >
  • &

For example, if you tried to do this:

<Button Width="100" Height="100" Content="Click &Me"/>

You would get these errors:

Error    5    Entity references or sequences beginning with an ampersand ‘&’ must be terminated with a semicolon ‘;’.   
Error    4    ‘"’ is an unexpected token. The expected token is ‘;’. Line 10, position 60.   

You can encode invalid characters for use in XAML by using the following encoding syntax:

Character

Encoding

<

&lt;

>

&gt;

&

&amp;

&quot;

space

&#160;

&apos;

(numeric character mappings)

&#[integer]; or

&#x[hex];

(nonbreaking space)

&#160; (assuming UTF-8 encoding)

So to fix the Button above you would insert “&amp;” wherever you want to use an “&”

<Button Width="100" Height="100" Content="Click &amp;Me"/>

Thank you,
–Mike Snow

Leave a Comment