Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

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:

  1. <
  2. >
  3. &

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
<

&lt;

>

&gt;

&

&amp;

&quot;

In addition, the following character encoding is also useful:

Character Encoding
Space

&#160;

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"></Button>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

If you try to use the following characters in a string in XAML you will get a slew of errors in your

# October 9, 2008 9:37 PM

Community Blogs said:

In this issue: Andrea Boschin, Jeff Handley, Mike Snow, Laurence Moroney, Terence Tsang, and Jesse Liberty

# October 10, 2008 12:42 AM

2008 October 10 - Links for today « My (almost) Daily Links said:

Pingback from  2008 October 10 - Links for today &laquo; My (almost) Daily Links

# October 10, 2008 4:05 AM

Silverlight news for October 10, 2008 said:

Pingback from  Silverlight news for October 10, 2008

# October 10, 2008 9:44 AM

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

# October 10, 2008 9:47 AM

robertlair said:

What if we want to use special characters with data binding?  I notice that if I try to set Text="{Binding Property}" where Property is m&amp;m, it actually comes through as m&amp;m instead of m&m.  I ask this because I would like to use unicode characters.  

For example:

&#09824;

# October 11, 2008 7:53 AM

robertlair said:

Re: Using special characters in Binding... Figured it out.  I took care of this with a value converter and applied the HtmlDecode().

# October 12, 2008 12:38 PM

Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 10, 2008 | Alvin Ashcraft's Morning Dew

# October 12, 2008 6:05 PM

Silverlight news for October 13, 2008 said:

Pingback from  Silverlight news for October 13, 2008

# October 13, 2008 7:55 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #66 Title: How to copy XAML for Silverlight from Expression Designer Silverlight

# November 3, 2008 1:41 PM

Toutes les astuces pour mieux utiliser Visual Studio said:

Si vous essayez d’utiliser les caractères suivants dans une chaîne en XAML, vous allez obtenir des erreurs dans votre liste d’erreurs :

# November 12, 2008 5:17 AM