Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing an array in 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/])

like image 854
Gilad Naaman Avatar asked Dec 05 '10 17:12

Gilad Naaman


3 Answers

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.

like image 115
Josh Avatar answered Oct 14 '22 01:10

Josh


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

string json = new JavaScriptSerializer().Serialize(arr);
like image 35
Marc Gravell Avatar answered Oct 14 '22 01:10

Marc Gravell


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

like image 33
Ta01 Avatar answered Oct 13 '22 23:10

Ta01