4

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

Cyborg
  • 1,244
  • 12
  • 12
  • How do you know it comes through? The quick watch window shows escape characters. – Chris Shain Oct 26 '12 at 02:55
  • I am trying to use string.IndexOf(myString) to locate the string on a page. Looking at the value of myString, it contains the extra carriage return – Cyborg Oct 26 '12 at 03:31

2 Answers2

1

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

Community
  • 1
  • 1
pylover
  • 7,670
  • 8
  • 51
  • 73
0

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.

Cyborg
  • 1,244
  • 12
  • 12