开发者

Serializing an array in C#

开发者 https://www.devze.com 2023-01-29 03:24 出处:网络
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 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);
}

Will create a simple XML file that is very easy to understand/modify. To deserialize it, use the Deserialize method.


Human readable? I'd go for JavaScriptSerializer; just:

string json = new JavaScriptSerializer().Serialize(arr);


It's a language file, I need to give copies to my co-workers so they can translate the file into other language

XML Serialization is ideal it sounds like based on the above statement


If the serialized array needs to be portable and editable in a text editor, then you can use XML or Json to serialize


best way to learn is look at how it's done with a xsd that serialize into a xml

starting point

0

精彩评论

暂无评论...
验证码 换一张
取 消