序列化在C#中的数组 [英] Serializing an array in C#
问题描述
C#:
我总是觉得很复杂的方式来序列的所有种类的物体,列表和谁知道,
,但我似乎无法找到一个简单的方法来序列化数组。
I always find very sophisticated way to serialize all kind of objects, lists and who knows, But I can't seem to find a simple way to serialize an array.
(我发现的有一个,但它的序列化阵列的二进制文件,我需要能够编辑序列化文件中的任何普通的文本编辑器[这是一个语言文件,我需要给拷贝到我的同事,使他们能够翻译文件转换成其他语言/])
(I found one, but its serializing the array to a binary file, and I need to be able to edit the serialized file in any regular text editor [It's a language file, I need to give copies to my co-workers so they can translate the file into other languages/])
推荐答案
假设你的数组的字符串数组...
Assuming your array is an array of strings...
using (var stream = File.Create("file.xml")) {
var serializer = new XmlSerializer(typeof(string[]));
serializer.Serialize(stream, someArrayOfStrings);
}
将创建一个简单的XML文件,这是非常容易理解/修改。反序列化它,使用Deserialize方法。
Will create a simple XML file that is very easy to understand/modify. To deserialize it, use the Deserialize method.
这篇关于序列化在C#中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!