Serialization in C#
-
I'm trying to serialize a complex data structure, and I have some questions. - How do I serialize an array? In the GetObjectData that I have to implement, I use the AddInfo function. But it takes two parameters : a "key" in a value. If I want to save a float[8], how can I do this? Do something like : for(int i = 0; i <8; i++) { string key = "Array" + i.ToString(); SerializeInfo.AddValue(key, array[i]); } - How to I serialize complex object? Suppose I have a class CDrawing, that cointains an array of CShape. A CShape can be derived into a CCircle or a CRectangle Somewhere in my code I do shapeArray[0] = new CCircle; shapeArray[1] = new CRectangle; Serializing is not a pb, because I will make the GetObjectData function virtual, so it will use the correct one to write CCircle parameters for the first shape, and CRectangle paramters for the second shape. But how can I deserialize? How do I know if I must do a new CCircle, or a new CRectanle? - How can I serialize to a text file? I've found only a binary and a SOAP formatter.
-
I'm trying to serialize a complex data structure, and I have some questions. - How do I serialize an array? In the GetObjectData that I have to implement, I use the AddInfo function. But it takes two parameters : a "key" in a value. If I want to save a float[8], how can I do this? Do something like : for(int i = 0; i <8; i++) { string key = "Array" + i.ToString(); SerializeInfo.AddValue(key, array[i]); } - How to I serialize complex object? Suppose I have a class CDrawing, that cointains an array of CShape. A CShape can be derived into a CCircle or a CRectangle Somewhere in my code I do shapeArray[0] = new CCircle; shapeArray[1] = new CRectangle; Serializing is not a pb, because I will make the GetObjectData function virtual, so it will use the correct one to write CCircle parameters for the first shape, and CRectangle paramters for the second shape. But how can I deserialize? How do I know if I must do a new CCircle, or a new CRectanle? - How can I serialize to a text file? I've found only a binary and a SOAP formatter.
Stephane David wrote: How can I serialize to a text file? If you want to serialize to a text file, you will have to come up with your own format (well, that is assuming you want to be able to read it with your own eyes). But if you are willing to just save an object or array out to disk and only have the computer read it, you must mark each of your classes with the
Serializable
attribute. If all the objects (Shapes for you) are serializable, then the array that contains them is automatically serializable. Serializing is not a particularly difficult thing, I would just need a bit more info on your classes... -Nathan --------------------------- Hmmm... what's a signature? -
Stephane David wrote: How can I serialize to a text file? If you want to serialize to a text file, you will have to come up with your own format (well, that is assuming you want to be able to read it with your own eyes). But if you are willing to just save an object or array out to disk and only have the computer read it, you must mark each of your classes with the
Serializable
attribute. If all the objects (Shapes for you) are serializable, then the array that contains them is automatically serializable. Serializing is not a particularly difficult thing, I would just need a bit more info on your classes... -Nathan --------------------------- Hmmm... what's a signature?The real classes are a bit to complexes to explain in details. But here is I think an exemple which illustrates the system well (warning : minimum syntax!) class CPoint { int X; int Y; } class CShape { string name; virtual void Draw(); } class CCircle : CShape { int Radius; CPoint center; override void Draw(); } class CPolygon : CShape { CPoint[] geometry; override void Draw(); } class CDrawing { public CShape[] shapes; } And now, I do this CDrawing drawing = new CDrawing(); drawing.shapes[0] = new CCircle(); // Initialize the circle //... drawing.shapes[1] = new CPolygon(); // Initialize the polygon //... Now, I want to save my drawing to a binary or text or XML file, and then load it.
-
The real classes are a bit to complexes to explain in details. But here is I think an exemple which illustrates the system well (warning : minimum syntax!) class CPoint { int X; int Y; } class CShape { string name; virtual void Draw(); } class CCircle : CShape { int Radius; CPoint center; override void Draw(); } class CPolygon : CShape { CPoint[] geometry; override void Draw(); } class CDrawing { public CShape[] shapes; } And now, I do this CDrawing drawing = new CDrawing(); drawing.shapes[0] = new CCircle(); // Initialize the circle //... drawing.shapes[1] = new CPolygon(); // Initialize the polygon //... Now, I want to save my drawing to a binary or text or XML file, and then load it.
Stephane David wrote:
class CPoint { int X; int Y; }
First: This little class is unneeded; check outSystem.Drawing.Point
. Second: You then need to mark your classes withSerializable
attributes.[Serializable(true)] class CShape { string name; virtual void Draw(); } [Serializable(true)] class CCircle : CShape { int Radius; CPoint center; override void Draw(); } [Serializable(true)] class CPolygon : CShape { CPoint[] geometry; override void Draw(); } [Serializable(true)] class CDrawing { public CShape[] shapes; } // functions for saving and loading using System.Runtime.Serialization.Formatters.Binary; using System.IO; void SaveToFile(string path,CDrawing drawing) { using(FileStream fs = File.OpenWrite(path)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs,drawing); } } CDrawing LoadFromFile(string path) { using(FileStream fs = File.OpenRead(path)) { BinaryFormatter bf = new BinaryFormatter(); return (CDrawing)bf.Deserialize(fs); } }
Hope this gets you on your way... -Nathan --------------------------- Hmmm... what's a signature? -
Stephane David wrote:
class CPoint { int X; int Y; }
First: This little class is unneeded; check outSystem.Drawing.Point
. Second: You then need to mark your classes withSerializable
attributes.[Serializable(true)] class CShape { string name; virtual void Draw(); } [Serializable(true)] class CCircle : CShape { int Radius; CPoint center; override void Draw(); } [Serializable(true)] class CPolygon : CShape { CPoint[] geometry; override void Draw(); } [Serializable(true)] class CDrawing { public CShape[] shapes; } // functions for saving and loading using System.Runtime.Serialization.Formatters.Binary; using System.IO; void SaveToFile(string path,CDrawing drawing) { using(FileStream fs = File.OpenWrite(path)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs,drawing); } } CDrawing LoadFromFile(string path) { using(FileStream fs = File.OpenRead(path)) { BinaryFormatter bf = new BinaryFormatter(); return (CDrawing)bf.Deserialize(fs); } }
Hope this gets you on your way... -Nathan --------------------------- Hmmm... what's a signature?The actual name of classes were just illustration, ht class architecture is muh more complex. I'll try what you say. It was what I read in the documentation, but I was doubtfull it would actually save an array of derived classes as the shapes member in my CDrawing class.