Cyborg
Cyborg

Reputation: 1244

How do you pass a string literal in from a settings file in C#

I want to create a configuration setting to pass in a string to my application. The string is based on some text from a web page which is the reason I need to allow for it to be changed from within a config file.

The string I want to pass in is

a)

Forecast Summary:</b> 
    <span class="phrase">

And the format of the string literal that works when I use it to search the page is

b)

string myString = "Forecast Summary:</b> \n        <span class=\"phrase\">";

the problem is that the passed in string (by pasting the text in (a) above into the App Settings screen ) comes through in the format

c)

"Forecast Summary:</b> \r\n        <span class=\"phrase\">"

(which has a carriage return inserted)

Is there a way to enter the string in the App.Config as the "exact" string literal

Upvotes: 4

Views: 3162

Answers (2)

Cyborg
Cyborg

Reputation: 1244

I first solved it by entering XML Encoded characters directly into the App.Config.

<setting name="DataExtractFrom" serializeAs="String">
     <value>Forecast Summary:&lt;/b&gt; &#10;        &lt;span class="phrase"&gt;</value>
</setting>

(Previously I had pasted it into the Settings screen for the project which must add the extra carriage return)

then

As pylover suggested above, it works if I edit the App.Config file using CDATA format ..

<setting name="DataExtractFrom" serializeAs="String">
   <value><![CDATA[Forecast Summary:</b> \n        <span class=\"phrase\">]]></value>
</setting>

This looks cleaner in the config file so I ended up using that format.

Upvotes: 0

pylover
pylover

Reputation: 8065

You need to use CDATA or XML Escape codes.

<myxml>
    <record>
        <![CDATA[
        Line 1 <br />
        Line 2 <br />
        Line 3 <br />
        ]]>
    </record>
</myxml>

see here for more info about XML Escaping

for more info see: here & here

Upvotes: 1

Related Questions