4

So I have an App.config file setup something like this:

<configuration>
    <appSettings>
        <add key="Description" value="My too long of a\n description that needs\n new lines so the entire\n thing can be seen."/>
    </appSettings>
</configuration>

Now, at run-time, I need to change the Text property of a Label to one of these many descriptions located in the App.config file. If I include the new line character in the App.config, the Label seems to ignore that it is a new line character and instead prints it out literally. However, if I were to remove these new line characters and insert them at run-time, then the Label will recognize them and insert new lines as it should.

My question is, why? Why does the Label ignore them and print them out literally if they come from the App.config?

To use this description from the App.config, this is all I'm doing:

myLabel.Text = ConfigurationManager.AppSettings["Description"];
Ricky L.
  • 246
  • 5
  • 13
  • 1
    `\n` denotes `\xa\xd` as string literal in C#, but in that xaml it is still just 2 characters. – Sinatr Oct 04 '17 at 15:24
  • 1
    Possible duplicate of [Adding a new line/break tag in XML](https://stackoverflow.com/questions/10917555/adding-a-new-line-break-tag-in-xml) – Sinatr Oct 04 '17 at 15:25
  • You are right, it is a duplicate. Thanks for pointing it out to me! @Sinatr – Ricky L. Oct 04 '17 at 15:27
  • This is a question about App.config, not just XML. I would argue against closing it as a duplicate @Sinatr – Joe Oct 04 '17 at 15:28
  • @Joe, well, duplicate is language agnostic question and [one of the answer](https://stackoverflow.com/a/10918223/1997232) there is exactly what you suggest. And then I thought about `string.Replace`... – Sinatr Oct 04 '17 at 15:33
  • The duplicate question wasn't Windows-specific (although the answer was). – Joe Oct 04 '17 at 15:47

2 Answers2

5

I would expect \r\n (return, newline) on a Windows system, not just newline. In any case, App.config is XML, so you want the XML escape sequences:

&#xD;&#xA;
Joe
  • 46,419
  • 33
  • 155
  • 245
2

If you want to keep "\n" in xml (for clarity?), then an easy fix:

myLabel.Text = ConfigurationManager.AppSettings["Description"].Replace("\\n","\xa\xd");
Sinatr
  • 20,892
  • 15
  • 90
  • 319