0

I have a WPF form, for some reason ONLY '&' character within string cannot be compiled with.

<TextBox>=948&showtree=1</TextBox> <!--Error due to char &-->

I have tried:<TextBox Text = @"=948&showtree=1"/> yet, same result.

I have tried adding "!@#$%^*()_+" it also only happens with '&'. What is so special about '&' and how to overcome this problem within xaml?

I could add it in .cs file but I want to know why did this occurred.

3 Answers3

2

The ampersand is a special character preceding other codes in XML - to use one you need to use the code for an ampersand which is &amp;

<TextBox>=948&amp;showtree=1</TextBox>
PaulF
  • 6,673
  • 2
  • 18
  • 29
2

& is a special character in XML, which needs to be specified as &amp;, just like &lt; for < and &gt; for >.

So your line should read:

<TextBox>=948&amp;showtree=1</TextBox>

For more information on special characters in XML you can read this: http://xml.silmaril.ie/specials.html

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • I came across &amp but typed it without ; at end. Thanks for quick reply. –  Jul 29 '15 at 08:38
1

You need to encode the ampersand.

Have you tried &amp; ?

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

Community
  • 1
  • 1
Anemoia
  • 7,928
  • 7
  • 46
  • 71