3

似乎 user.config 文件Properties.Settings可以使用 <value /> 标记正确处理空字符串值,但是仅由一个或多个空格组成的值(例如“”)不会存储为...

    <value>    </value>

而是作为...

     <value>
     </value>

因此包括 CRLF 和“缩进”空格。

如何保存一个或多个空格字符的设置?

4

3 回答 3

3

如果空格很重要,请使用 CDATA:

<value><![CDATA[    ]]></value>
于 2013-01-22T16:23:04.440 回答
2

感谢这些建议,但都没有解决问题。使用 CDATA 意味着下次将属性加载到程序中时(例如string prop = Properties.Settings.Default.MyProp),您将获得 CDATA 字符串(例如 prop is now "<![CDATA...")。如果使用&nbsp;. 顺便说一句,PSDSave() 将 &、< 和 > 自动转换为&amp;, &lt;, and &gt;实体。

基本上,如果字符串(和字符)设置为空或包含至少一个非空格字符,则字符串(和字符)设置将被正确保存和恢复,但一个或多个空格的字符串(或一个空格的字符)未正确保存。

这个问题出现在允许用户设置字符串用作行缩进前缀(例如"----", " ",等)的程序中。我已经修改了程序以避免这个空格字符串错误,但在我看来,空格字符串仍然应该是一个完全有效的“属性”!

于 2013-01-23T18:39:25.813 回答
0

For others which will looking for how to save 'SPACE' in config...

You can simple use it's decimal value from ASCII table (http://www.asciitable.com/). So for space character it's value 32. Some examples...:

EXAMPLE 1:

Save single space in App.config

config section:

<setting name="OneSpaceString" serializeAs="String">
    <value>32</value>
</setting>

Application section:

char space = Convert.ToChar(int.Parse(Properties.Settings.Default.OneSpaceString));
string stringSpace = space.ToString();

EXAMPLE 2:

Save multi space string in config (there is a lot of ways how to do it, but for example...)

Config section

<setting name="MultiSpaceString" serializeAs="String">
    <value>32;32;32;32</value>
</setting>

Application section:

char[] spaces = Properties.Settings.Default.MultiSpaceString.GetCharsByDecimal(new char[] { ';' });
string stringSpaces = string.Join(string.Empty, spaces);

//Extension method for getting chars by decimal from our string
public static char[] GetCharsByDecimal(this string inputString, char[] delimiters)
    {
        int[] charsDecimals = inputString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s)).ToArray();
        char[] resultChars = new char[charsDecimals.Length];

        for (int i = 0; i < charsDecimals.Length; i++)
        {
            resultChars[i] = Convert.ToChar(charsDecimals[i]);
        }

        return resultChars;
    }

//Or safer variant of our extension method
public static char[] GetCharsByDecimal(this string inputString)
    {
        int[] charsDecimals = Regex.Split(inputString, @"\D+").Select(s => int.Parse(s)).ToArray();
        char[] resultChars = new char[charsDecimals.Length];

        for (int i = 0; i < charsDecimals.Length; i++)
        {
            resultChars[i] = Convert.ToChar(charsDecimals[i]);
        }

        return resultChars;
    }

EXAMPLE 3:

Save delimiters (including space) in config

Config section

<setting name="Delimiters" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>59</string>
                        <string>44</string>
                        <string>9</string>
                        <string>45</string>
                        <string>124</string>
                        <string>32</string>
                    </ArrayOfString>
                </value>
            </setting>

Application section

char[] delimiters = Properties.Settings.Default.Delimiters.Cast<string>().Select(s => Convert.ToChar(int.Parse(s))).ToArray();
于 2019-02-20T07:46:21.523 回答