storing data
-
Hey, just wondering what the best method would be for storing simple objects in an mysql table.
public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }
-
Hey, just wondering what the best method would be for storing simple objects in an mysql table.
public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }
-
Hey, just wondering what the best method would be for storing simple objects in an mysql table.
public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }
-
Hey, just wondering what the best method would be for storing simple objects in an mysql table.
public class Address { public int Number { get; set; } public int Unit { get; set; } public string Street { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }
Ok, well i managed to serialize the object using a binaryformatter and then base64'd the result.
public string Serialize() { MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); Serializer.Serialize(MemStream, this); return Encoding.ASCII.GetString(MemStream.ToArray()).ToBase64(); }
but im having some issues when deserializing: "End of stream encountered before parsing was completed."public static Address Deserialize(string SerializedData) { Address Address; MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); string Decoded = SerializedData.FromBase64(); byte[] Buffer = Encoding.ASCII.GetBytes(Decoded); MemStream.Write(Buffer, 0, Buffer.Length); Address = (Address)Serializer.Deserialize(MemStream); <-- fails right here... return Address; }
-
Ok, well i managed to serialize the object using a binaryformatter and then base64'd the result.
public string Serialize() { MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); Serializer.Serialize(MemStream, this); return Encoding.ASCII.GetString(MemStream.ToArray()).ToBase64(); }
but im having some issues when deserializing: "End of stream encountered before parsing was completed."public static Address Deserialize(string SerializedData) { Address Address; MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); string Decoded = SerializedData.FromBase64(); byte[] Buffer = Encoding.ASCII.GetBytes(Decoded); MemStream.Write(Buffer, 0, Buffer.Length); Address = (Address)Serializer.Deserialize(MemStream); <-- fails right here... return Address; }
Basically, what you're missing is seeking the stream pointer to the beginning after writing to the memstream but before calling Deserialize(). Try
public string Serialize() { MemoryStream MemStream = new MemoryStream(); BinaryFormatter Serializer = new BinaryFormatter(); Serializer.Serialize(MemStream, this); return Convert.ToBase64String(MemStream.ToArray()); } public static Address Deserialize(string SerializedData) { byte\[\] Buffer = Convert.FromBase64String(SerializedData); MemoryStream MemStream = new MemoryStream(Buffer); BinaryFormatter Serializer = new BinaryFormatter(); return (Address)Serializer.Deserialize(MemStream); }
*edit* condensed Deserialize() a bit
:)Mark Salsbery Microsoft MVP - Visual C++ :java:
modified on Wednesday, July 30, 2008 6:29 PM