[ACCEPTED]-In C#, How can I serialize Queue<>? (.Net 2.0)-.net-2.0

Accepted answer
Score: 15

It would be easier (and more appropriate 3 IMO) to serialize the data from the queue - perhaps 2 in a flat array or List<T>. Since Queue<T> implements IEnumerable<T>, you 1 should be able to use:

List<T> list = new List<T>(queue);
Score: 1

Not all parts of the framework are designed 6 for XML serialization. You'll find that 5 dictionaries also are lacking in the serialization 4 department.

A queue is pretty trivial to 3 implement. You can easily create your own 2 that also implements IList so that it will 1 be serializable.

Score: 0

if you want to use the built in serialization 5 you need to play by its rules, which means 4 default ctor, and public get/set properties 3 for the members you want to serialize (and 2 presumably deserialize ) on the data type 1 you want to serialize (MyData)

Score: 0

In my case i had a dynamic queue and i had 2 to save and load the state of this one.

Using 1 Newtonsoft.Json:

List<dynamic> sampleListOfRecords = new List<dynamic>();
Queue<dynamic> recordQueue = new Queue<dynamic>();
//I add data to queue from a sample list
foreach(dynamic r in sampleListOfRecords)
{
     recordQueue.Enqueue(r);
}

//Serialize
File.WriteAllText("queue.json",
     JsonConvert.SerializeObject(recordQueue.ToList(), Formatting.Indented));
//Deserialize
List<dynamic> data = 
     JsonConvert.DeserializeObject<List<dynamic>>(File.ReadAllText("queue.json"));

More Related questions