Escape & in App.config

developer747 picture developer747 · Jun 4, 2012 · Viewed 42.6k times · Source

I have the following piece of config in my App.config file:

<add key="ConnectionString" 
     value="INITIAL CATALOG=myDatabase;DATA SOURCE=myServer;User ID=myUser;Password=!&myPass99;"/>

The above connection string is rejected by Visual Studio intellisense. It says entity myPass99 not defined. The problem is caused by the & in the password.

How do I escape it?

Answer

jglouie picture jglouie · Jun 4, 2012

Escape the ampersand by using &amp; in place of the &

As an FYI, see here for further characters: What characters do I need to escape in XML documents?