Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppConfig key value pair

Tags:

.net

In my application there is a requirement to remove special characters from a string. I am able to achieve this by reading a string of special chars from config appsettinf key-value pair.

EX :

<appSettings>
<add key="ExcludeSpecialChars" value ="%'^"/>
</appSettings>

I am facing problem when I include & in the value ex :

key="ExcludeSpecialChars" value ="&%'^"

The application fails to build and shows an error "cannot parse the entity".

Is there any workaround so that I can include & in the special chars string?

like image 351
Anks4SomeMore Avatar asked Nov 11 '11 03:11

Anks4SomeMore


2 Answers

Since .NET .config files are XML, use the entity &amp; to represent an ampersand:

<appSettings>
    <add key="ExcludeSpecialChars" value ="&amp;%'^"/>
</appSettings>
like image 109
Lance U. Matthews Avatar answered Nov 20 '22 12:11

Lance U. Matthews


Some characters have a special meaning in XML.

If you place a character like "&" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

you can use &amp; there are 5 predefined entity references in XML

&lt;    <   less than
&gt;    >   greater than
&amp;   &   ampersand 
&apos;  '   apostrophe
&quot;  "   quotation mark

ref :http://www.w3schools.com/xml/xml_syntax.asp

like image 27
Damith Avatar answered Nov 20 '22 12:11

Damith