Escape { and } characters in config file

L

Lonifasiko

I'm developing a Winforms application that reads some variable values
from a .config file(similar to XML file). Besides, I need to format the
value I read from the config file. I do it this way:

String.Format(ConfigurationManager.AppSettings["Javascript"], "6) //
Want to insert "6" value

I've got this in my .config file:

<add key ="Javascript" value="function GetKey(){key = new
Key('myKey'){0};var i = 'jjj';} " />

First, the encoding of the .config file was set to utf-8, but framework
threw me an error because didn't like single quotes. Then, I changed
the encoding to ISO-8859-1 and single quotes problem was solved. But
another problem has now appeared: don't like both { and } characters.

How can I escape { and } characters? Or maybe a neutral
encoding/escaping method that lets me read all values properly? Is
there any way I can get in my code this "as is":

function GetKey(){key = new Key('myKey')6;var i = 'jjj';}

Please don't know what else I can try........

Thanks very much in advance.
 
K

Kevin Spencer

A configuration file is an XML document, so you have a couple of choices.

You can use a CDATA section in the configuration section that holds the
code. Anything inside a CDATA section is legal.

You can use encoding to indicate the character. A character reference in XML
is the sequence "" in which "nnn" is the decimal ASCII character code.
So, '{' and '}' would be, for example, "{" and "}" (without the
quotes).

There are a couple of other ways as well, but one of those 2 should do it
for you.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Big thicks are made up of lots of little thins.
 
B

Barry Kelly

Kevin Spencer said:
A configuration file is an XML document, so you have a couple of choices.

You can use a CDATA section in the configuration section that holds the
code. Anything inside a CDATA section is legal.

You can use encoding to indicate the character. A character reference in XML
is the sequence "" in which "nnn" is the decimal ASCII character code.
So, '{' and '}' would be, for example, "{" and "}" (without the
quotes).

I've never seen entities required inside attribute values. Interesting.

-- Barry
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The apostrophes can be encoded using the &apos; entity.

I don't find any obvious reason why the brackets would be any problem,
though. I suggest that you change back to UTF-8 and encode the apostrophes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top