Anks4SomeMore
Anks4SomeMore

Reputation: 81

AppConfig key value pair

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?

Upvotes: 6

Views: 1775

Answers (2)

Damith
Damith

Reputation: 63065

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

Upvotes: 5

Lance U. Matthews
Lance U. Matthews

Reputation: 16596

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

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

Upvotes: 6

Related Questions