what is the best way to save a list of strings to the settings file? i've seen some solutions of editing the xml setting file type to string[], but i can get that to work in the editor.
将字符串列表保存到设置文件的最佳方法是什么?我已经看到了一些将xml设置文件类型编辑为string []的解决方案,但我可以在编辑器中使用它。
i also don't know how many strings are going to get added at runtime and it's going to be a lot so i don't want to define a bunch of named strings here. but i still want to save them and have access the next time the application is opened.
我也不知道在运行时会添加多少字符串,这将是很多,所以我不想在这里定义一堆命名字符串。但我仍然希望保存它们,并在下次打开应用程序时访问它们。
i see there is a System.Collections.Specialized.StringCollection
in the settings types, is this what i want? if there's a way to save an array to the XML that would work too.
我看到设置类型中有一个System.Collections.Specialized.StringCollection,这是我想要的吗?如果有办法将数组保存到XML也可以工作。
2 个解决方案
#1
1
Here is a simple XML writer script. (untested)
这是一个简单的XML编写器脚本。 (另)
var stringArray = new string[100]; //value of the string
var stringArrayIdentifier = new string[100]; //refers to what you will call the field
//<identifier>string</identifier>
var settings = new XmlWriterSettings
{Indent = true, IndentChars = "\t", NewLineOnAttributes = false};
using (XmlWriter writer = XmlWriter.Create("PATH", settings))
{
writer.WriteStartDocument();
foreach (int i = 0; i < stringArray.Length; i++)
{
writer.WriteStartElement(stringArrayIdentifier[i]);
writer.WriteString(stringArray[i]);
writer.WriteEndElement();
}
writer.WriteEndDocument();
}
JavaScriptSerializer.Serialize() will do what you want as well, but with limited use. If all you want is simple, use JavaScriptSerializer.Serialize().
JavaScriptSerializer.Serialize()也会做你想要的,但用途有限。如果您想要的只是简单,请使用JavaScriptSerializer.Serialize()。
#2
0
Perhaps this question How to store int[] array in application Settings will be of help. Tho if you're looking for a quick and easy way to do it, you could try just serializing the collection to json.
也许这个问题如何在应用程序设置中存储int []数组将有所帮助。如果您正在寻找一种快速简便的方法,那么您可以尝试将集合序列化为json。
var serializer = new JavaScriptSerializer();
var myArray = new []{"one", "two", "three"};
var myString = serializer.Serialize(myArray);
// Now you can store myString in the settings file...
#1
1
Here is a simple XML writer script. (untested)
这是一个简单的XML编写器脚本。 (另)
var stringArray = new string[100]; //value of the string
var stringArrayIdentifier = new string[100]; //refers to what you will call the field
//<identifier>string</identifier>
var settings = new XmlWriterSettings
{Indent = true, IndentChars = "\t", NewLineOnAttributes = false};
using (XmlWriter writer = XmlWriter.Create("PATH", settings))
{
writer.WriteStartDocument();
foreach (int i = 0; i < stringArray.Length; i++)
{
writer.WriteStartElement(stringArrayIdentifier[i]);
writer.WriteString(stringArray[i]);
writer.WriteEndElement();
}
writer.WriteEndDocument();
}
JavaScriptSerializer.Serialize() will do what you want as well, but with limited use. If all you want is simple, use JavaScriptSerializer.Serialize().
JavaScriptSerializer.Serialize()也会做你想要的,但用途有限。如果您想要的只是简单,请使用JavaScriptSerializer.Serialize()。
#2
0
Perhaps this question How to store int[] array in application Settings will be of help. Tho if you're looking for a quick and easy way to do it, you could try just serializing the collection to json.
也许这个问题如何在应用程序设置中存储int []数组将有所帮助。如果您正在寻找一种快速简便的方法,那么您可以尝试将集合序列化为json。
var serializer = new JavaScriptSerializer();
var myArray = new []{"one", "two", "three"};
var myString = serializer.Serialize(myArray);
// Now you can store myString in the settings file...