C# Way to serialize List<string> in settings containing any character (Regex/xml)

安稳与你 提交于 2019-12-10 18:46:31

问题


I am looking for a neat/clean way to store a list of strings into a C# settings file. As far as I can work out, you can't store List objects into these settings, so basically it needs to be converted to a string. For example, say I have a list of names:

  • NameA
  • Name;B
  • Complex, Weird, Name
  • Name"nickname"Person

i.e. I am trying to demonstrate a list of names which can possibly contain any character. Does anyone have any recommendations for a neat format + Regex to read it that can handle any character? Or possibly an easy way to serialize a List<string>?

Currently, I am saving them as a simple command delimited string which works fine so long as you are careful with the names (can't have commas), but is destined to break down the line.


回答1:


You can select the type of your setting entry as

System.Collections.Specialized.StringCollection

from the Properties.Settings tab.

This will be translated into your config file as something like

 <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string>string1</string>
  <string>string2</string>
 </ArrayOfString>


来源:https://stackoverflow.com/questions/1700779/c-sharp-way-to-serialize-liststring-in-settings-containing-any-character-rege

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!