在.Net 资源文件(App_GlobalResources)中回车/行提要

我在 App_GlobalResources.resx文件里保存了几条短信。

文本必须是多行,我需要他们包含行饲料。但是,当我读取内容时,所有行提要都消失了(\r\n被打印,而不是作为 CRLF 10 13控制字符)。

我知道当我阅读内容时,我可以通过将 \r\n(或者其他任何东西)重新替换回 CRLF 来解决这个问题,但是我想知道为什么这些明显以文本为目标的 resx 文件忽略了控制字符——而 CRLF 有点重要——如果有人知道是否有一个设置或者什么东西可以让它自然工作。

65921 次浏览

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>

Use Shift+Enter to insert a new line.

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.

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.

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>