简体   繁体   English

C#MVC,从web.config读取回车符

[英]C# MVC, reading carriage returns from web.config

I am trying to put a list of delimiters in a web.config file, one of the delimiters is a carriage return - \\r\\n . 我试图在web.config文件中放置一个分隔符列表,其中一个分隔符是一个回车符 - \\r\\n The web.config entry looks like this - web.config条目如下所示 -

<add key="Separators" value=" |,|;|\r\n"/>

I am trying to read in the list using a call like the following - 我想尝试使用如下调用在列表中读取 -

string[] MySeparators = ConfigurationManager.AppSettings ConfigSettings.PartNumberSeparators].Split('|');

The list is being read, but the carriage return ends up containing extra back slashes and looks like this - \\\\r\\\\n 正在读取列表,但回车最终包含额外的反斜杠,看起来像这样 - \\\\r\\\\n

Is there some way to prevent this from happening? 有没有办法防止这种情况发生?

You could try using XML entities &#10; 您可以尝试使用XML实体&#10; and &#13; &#13; . Provided that the .NET configuration system supports resolving these entities, then these should appear as \\r\\n at runtime. 如果.NET配置系统支持解析这些实体,那么它们应该在运行时显示为\\r\\n

First, you forgot the | 首先,你忘记了| between \\n and \\r 介于\\ n和\\ r之间

Try writing a var like 尝试编写类似的var

>var test = "\";

this will give you an error, options are: 这会给你一个错误,选项是:

>var test = "\\";

>var test = @"\";

Try the second one 尝试第二个

您可以使用System.Text.RegularExpressions.Regex.Unescape ,这样:

string[] MySeparators = Regex.Unescape(ConfigurationManager.AppSettings.ConfigSettings.PartNumberSeparators]).Split('|');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM