.Net 资源文件 (App_GlobalResources) 中的回车/换行

发布于 2024-07-22 05:02:45 字数 322 浏览 2 评论 0原文

我将多个文本保存在 App_GlobalResources.resx 文件中。

文本必须是多行的,我需要让它们包含换行符。 但是,当我阅读内容时,所有换行符都消失了(打印 \r\n ,而不是 CRLF 10 13 控制字符)。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不打扰别人 2024-07-29 05:02:45

我使用 VB.NET Express Edition 来测试这一点。

在资源编辑器中(您可以在其中指定资源名称和字符串内容)将字符串内容以 Shift+Enter 分隔。

假设您要输入

hello  
world

“hello”,然后输入 Shift+Enter 和“world”。

如果您查看 Resources.Resx 文件(这是一个 xml 文件),您可以看到它创建了一个具有属性 xml:space="preserve" 的节点。

第二个选项

此外,您可以手动编辑Resources.resx并将内容修改为CDATA部分下的内容。

假设您有名为“example”的字符串。 在 Resources.resx 中搜索它并更改内容以在其中包含 CDATA 部分,而不是使用简单的值。

例如

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

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>
夜唯美灬不弃 2024-07-29 05:02:45

使用 Shift+Enter 插入新行。

Use Shift+Enter to insert a new line.

东走西顾 2024-07-29 05:02:45

使用 resx 设计器界面时

  • 如果您实际上将文本输入到 resx 文件中,那么您将使用

    Shift+Enter

    如其他答案中所述。

  • 如果您在 resx 中粘贴文本 - Visual Studio 将以与现有格式相同的格式粘贴文本(包括换行符/多行)。

打开XML格式的resx文件时

(使用查找和替换找到resx文件。当您从“查找结果”面板中单击该文件时,VS将打开XML格式的resx文件)

在这里您可以根据需要添加文本(在值标签中)和格式将被保留。

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.

七颜 2024-07-29 05:02:45

好吧,在我的情况下有效的是使用像这样的
标签:

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

这里有一篇包含更多信息的帖子:在 resx 资源文件中添加换行符

如果您碰巧在 ASP.NET 中使用 Razor 视图引擎您需要使用 MVC:

@Html.Raw(ResourceFile.ResourceString)

以便将
打印为 HTML。

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.

戏剧牡丹亭 2024-07-29 05:02:45

可以使用文本编辑器编辑*.resx 文件来添加换行符。

您甚至可以在 Visual Studio 中执行此操作:

  • 右键单击资源文件
  • 单击打开方式...
  • 选择带编码的 XML(文本)编辑器
  • 单击确定< /code>
  • 再次单击确定 进行编码选择(自动检测)
  • 搜索文本的名称(键)(例如“MY_TEXT”)
  • 编辑内的文本 标签。 对于换行符,只需按 Enter 即可。 注意:删除换行后的前导空格。 否则它们也会被插入。

使用 Visual Studio 2017 进行测试。

示例:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文