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,521
  • 5
  • 40
  • 64
Gilad Naaman
  • 6,390
  • 15
  • 52
  • 82
  • 1
    Uhm,... you need it to be human-readable? Why not use JSON-like syntax for the serialization? Or XML? – salezica 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
  • 68,005
  • 14
  • 144
  • 156
6

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

string json = new JavaScriptSerializer().Serialize(arr);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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
  • 31,040
  • 13
  • 70
  • 99
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
  • 17,397
  • 7
  • 55
  • 80
1

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

starting point

Fredou
  • 19,848
  • 10
  • 58
  • 113