7

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/])

jv42
  • 8,440
  • 4
  • 39
  • 62
Gilad Naaman
  • 6,078
  • 14
  • 49
  • 79
  • 1
    Uhm,... you need it to be human-readable? Why not use JSON-like syntax for the serialization? Or XML? – slezica Dec 05 '10 at 17:24

5 Answers5

12

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.

Josh
  • 66,410
  • 14
  • 139
  • 154
6

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

string json = new JavaScriptSerializer().Serialize(arr);
Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
2

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

Ta01
  • 30,248
  • 12
  • 69
  • 98
2

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

Carlos Muñoz
  • 16,721
  • 7
  • 55
  • 78
1

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

starting point

Fredou
  • 19,450
  • 9
  • 55
  • 111