Serialization problem
-
Hello. So these are my problems: I have to find a way to save a class of mine to a file. I found serialization and I thought it might work. But... When I tried BinarySerialization, some chars would just disapear, and I'd get an exception for "not a correct format". XmlSerialization is throwing non-stopable exceptions, I just went to the one after. I tried SOAP serialization, but now I came across another problem. I need to serialize a generic collection which I built. This gives me an exception, saying that it cannot serialize a generic class. (that's after successfuly serializing List). Another thing that I have with SOAP serialization is that i'm not sure that it'll work in Linux (I was unable to install mono), since I need to import the SOAP class from Windows folder. Can anyone recommend me on what to do? Waiting for your help. NaNg. :)
-
Hello. So these are my problems: I have to find a way to save a class of mine to a file. I found serialization and I thought it might work. But... When I tried BinarySerialization, some chars would just disapear, and I'd get an exception for "not a correct format". XmlSerialization is throwing non-stopable exceptions, I just went to the one after. I tried SOAP serialization, but now I came across another problem. I need to serialize a generic collection which I built. This gives me an exception, saying that it cannot serialize a generic class. (that's after successfuly serializing List). Another thing that I have with SOAP serialization is that i'm not sure that it'll work in Linux (I was unable to install mono), since I need to import the SOAP class from Windows folder. Can anyone recommend me on what to do? Waiting for your help. NaNg. :)
I don’t really understand what the problem is you are experiencing but I could tell you how I serialize into a binary stream I stream can be send or saved to file. Serializing to XML goes the same way. Note both serializer and deserializer and the object definition of the object to serialize should be the same at the sending and receiving end. To ensure this I simply put the classes in a dll which both sides use. With dotnet 1.x the formatter will give an exception when a version difference is detected. In dotnet 2.0 the formatter will serialize everything it recognizes even if there are version differences. The class to serialize should be declared with [Serializable] /// /// Serialize object to byte array, object must be Serializable /// /// object to serialize /// array public byte[] Serialize(T someobject) { byte[] objectstream=null; BinaryFormatter f = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); f.Serialize(stream, someobject); stream.SetLength( stream.Position); objectstream = stream.GetBuffer(); return objectstream; } public void Deserialize(byte[] stream, out T someobject) { BinaryFormatter f = new BinaryFormatter(); MemoryStream serstream = new MemoryStream(stream); someobject = (T)f.Deserialize(serstream); } Class def of class to serialize: [Serializable] public class SetConfigMessage { public string Name = ""; public string Value = ""; /// /// Config message /// /// /// public SetConfigMessage() { } public SetConfigMessage(string name, string value) { this.Name = name; this.Value = value; } } madv113
-
I don’t really understand what the problem is you are experiencing but I could tell you how I serialize into a binary stream I stream can be send or saved to file. Serializing to XML goes the same way. Note both serializer and deserializer and the object definition of the object to serialize should be the same at the sending and receiving end. To ensure this I simply put the classes in a dll which both sides use. With dotnet 1.x the formatter will give an exception when a version difference is detected. In dotnet 2.0 the formatter will serialize everything it recognizes even if there are version differences. The class to serialize should be declared with [Serializable] /// /// Serialize object to byte array, object must be Serializable /// /// object to serialize /// array public byte[] Serialize(T someobject) { byte[] objectstream=null; BinaryFormatter f = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); f.Serialize(stream, someobject); stream.SetLength( stream.Position); objectstream = stream.GetBuffer(); return objectstream; } public void Deserialize(byte[] stream, out T someobject) { BinaryFormatter f = new BinaryFormatter(); MemoryStream serstream = new MemoryStream(stream); someobject = (T)f.Deserialize(serstream); } Class def of class to serialize: [Serializable] public class SetConfigMessage { public string Name = ""; public string Value = ""; /// /// Config message /// /// /// public SetConfigMessage() { } public SetConfigMessage(string name, string value) { this.Name = name; this.Value = value; } } madv113