在 XML 设置文件中创建字符串列表

发布于 2025-01-01 09:14:24 字数 271 浏览 1 评论 0原文

将字符串列表保存到设置文件的最佳方法是什么?我已经看到了一些将 xml 设置文件类型编辑为 string[] 的解决方案,但我可以让它在编辑器中工作。

我也不知道在运行时会添加多少字符串,而且会很多,所以我不想在这里定义一堆命名字符串。但我仍然想保存它们并在下次打开应用程序时访问。

我看到设置类型中有一个 System.Collections.Specialized.StringCollection,这是我想要的吗?如果有一种方法可以将数组保存到 XML 中也可以。

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.

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.

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

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

发布评论

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

评论(2

獨角戲 2025-01-08 09:14:24

这是一个简单的 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() 也会做你想做的事情,但用途有限。如果您想要的只是简单,请使用 JavaScriptSerializer.Serialize()。

Here is a simple XML writer script. (untested)

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().

深海少女心 2025-01-08 09:14:24

也许这个问题How to store int[] array in application Settings 会有帮助。不过,如果您正在寻找一种快速简便的方法来完成此操作,您可以尝试将集合序列化为 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...

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.

var serializer = new JavaScriptSerializer();
var myArray = new []{"one", "two", "three"};
var myString = serializer.Serialize(myArray);
// Now you can store myString in the settings file...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文