简体   繁体   中英

Characters Escaping issue

I am working on WPF application and now I have part of my code that has to add content to my app.config file.

Simply said I have to add a Key-Value pair as a child of a particular element but when I try to add it the escaping symbols are changing and I dont know why.

I am giving you an example:

I want to add :

<add key="MyKey" value="&lt;add name=&quot;{0}&quot; /&gt;_#ss_dsa">

after the appSettings tag.When I am doing it manualy it works but when I try to add it by code-behind I am doing this:

 var appPath=Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
 var appConfigFile = Path.Combine( appPath, "MyApp.exe" ); 
 var config= ConfigurationManager.OpenExeConfiguration(appConfigFile);

and then:

config.AppSettings.Settings.Add("Key","Value");

is supposed to add <add key="Key" value="Value">

to the app.config AND IT DOES, but when I pass this:

"&lt;add name=&quot;{0}&quot; /&gt;_#ss_dsa"

for the value it transforms it into :

"&amp;lt;add name=&amp;quot;{0}&amp;quot; /&amp;gt;_#ss_dsa"

in the app.config file.I don't really know why is this happening and how can I fix it. It would be great if someone could give an advise.

The value expected is unescaped. So add it unescaped: try adding:

blah.Add("MyKey", @"<add name=""{0}"" />_#ss_dsa");

where the "" in the middle are C# escaping - the actual string here is

<add name="{0}" />_#ss_dsa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM