Carriage Return/Line Feed in .Net Resource File (App_GlobalResources)

64,376

Solution 1

I used VB.NET Express Edition to test this.

In the resource editor (where you can specify the name of the resource and string content) put the string content separated by Shift+Enter.

Lets say you want to type in

hello  
world

Type "hello" followed by Shift+Enter and "world".

If you look at the Resources.Resx file (which is an xml file), you can see that it creates a node with the attribute xml:space="preserve".

2nd option

Also, you can edit the Resources.resx manually and modify the content to be under CDATA section.

Assume that you have the string named "example". Search for it in Resources.resx and change the content to have CDATA section inside it as against having a simple value.

e.g.

<data name="example">
<![CDATA[
hello
world
1
2   3
4
]]>  </data>

Solution 2

Use Shift+Enter to insert a new line.

Solution 3

When using the resx designer interface

  • If you are actually typing the text into the resx file then you would use

    Shift+Enter

    as noted in other answers.

  • If you are pasting text in the resx - Visual Studio will paste the text in the same format as it already is (including linebreaks / multiline).

When opening the resx file in XML format

(locate the resx file using find and replace.. when you click the file from the 'find results' panel, VS will open the resx file in XML)

Here you can add text as you like (in value tags) and formatting will be preserved.

Solution 4

Well, what worked in my situation was using a <br> tag like this:

A text with a line break <br> and this goes in the second line.

There's a post with more info here: Putting a line break in an resx resource file

If you happen to be using Razor view engine with ASP.NET MVC you need to use:

@Html.Raw(ResourceFile.ResourceString)

so that it prints the <br> as HTML.

Solution 5

It's possible to edit the *.resx file with a text editor to add linebreaks.

You can do it even within Visual Studio:

  • Right click to the resource file
  • Click to Open with ...
  • Select XML (Text) Editor with Encoding
  • Click OK
  • Click OK again for encoding selection (auto-detect)
  • Search for the name (key) of your text (e.g. "MY_TEXT")
  • Edit the text inside of the <value> tag. For linebreaks just push Enter. Note: Remove the leading spaces after linebreak. Otherwise they are inserted, too.

Tested with Visual Studio 2017.

Example:

  <data name="MY_TEXT" xml:space="preserve">
    <value>Line 1
Line 2
Line 3</value>
  </data>
Share:
64,376

Related videos on Youtube

Alex
Author by

Alex

Updated on December 18, 2020

Comments

  • Alex
    Alex over 3 years

    I'm keeping several texts in an App_GlobalResources.resx file.

    The texts have to be multi-line and I need to have them contain line feeds. However, when I read the contents, all line feeds are gone (\r\n is printed, not as CRLF 10 13 control character).

    I know that I could work around this by re-replacing \r\n (or anything else for that matter) back to CRLF when I read the contents, but I wondered why these clearly text-targeted resx files ignore control characters - and CRLF is kind of important - and if anybody knows if there's a setting or something that would enable this to work naturally.

  • Jon O
    Jon O almost 12 years
    I found this answer looking for a way to insert \t tabs. I had to actually go into visual studio's settings for tabs in xml files and turn off the "tabs as spaces" option (where it replaces tab characters with a number of spaces instead) so that I could get a literal tab character into my string.
  • Leniel Maccaferri
    Leniel Maccaferri almost 12 years
    Fantastic... didn't know it was possible! :)
  • Illuminati
    Illuminati over 11 years
    who would have guessed. tried ctrl +enter, alt + enter. consistency MS!
  • Andez
    Andez over 9 years
    Better late than never... After I had assumed \n was the correct way. Only 50 changes to make!
  • Peter Meyer
    Peter Meyer about 9 years
    This will work if your resource string is destined for use as HTML. But if the string is to be used in a non-HTML manner (as the string for a tooltip or error message dialog in a desktop app for example), you'll just get <br> in your string.
  • xyzWty
    xyzWty over 5 years
    Saved my day. Thank you!!
  • Arno 2501
    Arno 2501 over 3 years
    Yes! But you might need to format it like that: <br/> please notice the slash before closing the tag so it's XML compliant (because all xml tags need a closing tag).