0

I am trying to serialize a class object using binary serialization in C#. I have tried and everywhere all I can find that the serialized data goes to a file always in all the examples I have seen.

In my case, I have to store the serialized data in SQL. The following is an example of the method I have created.

//Serializing the List
public void Serialize(Employees emps, String filename)
{
    //Create the stream to add object into it.
    System.IO.Stream ms = File.OpenWrite(filename); 
    //Format the object as Binary

    BinaryFormatter formatter = new BinaryFormatter();
    //It serialize the employee object
    formatter.Serialize(ms, emps);
    ms.Flush();
    ms.Close();
    ms.Dispose();
}

How can I get the serialized data directly in a string variable? I don't want to use a file.

Please help.

dbc
  • 104,963
  • 20
  • 228
  • 340
Rober
  • 73
  • 1
  • 8
  • 3
    Binary serialization and strings are incompatible. Why do you want it in a string? Because then you should base64 encode it. Alternatively, just use a VARBINARY column. – CodeCaster Nov 25 '19 at 13:55
  • You cannot !!! Using string methods on binary data will corrupt the data. c# requires using encoding to convert binary data to a string and encoding will change or delete bytes that do not meet the encoding. From example using UTF8 encoding will eliminate the byte 0x80. – jdweng Nov 25 '19 at 14:11
  • 1
    I cannot emphasize this enough: `BinaryFormatter` **is not** intended for this scenario; in fact, for most purposes you should consider `BinaryFormatter` obsolete and deprecated - there are **lots** of known reasons not to use it, and many attempts have been made to formally deprecate it. It isn't a case of "will this hurt me?" - it is **only** a case of "**when**". If you just want to store data, there are **lots** of other serializers - binary and text-based, which do not have the huge red warning flags that `BinaryFormatter` does. Happy to offer suggestions if you want. – Marc Gravell Nov 25 '19 at 16:38
  • Following up on the comment above, you might take a look at [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182). – dbc Nov 26 '19 at 20:18

2 Answers2

2

Just use MemoryStream ms = new MemoryStream() instead of your file stream. You can extract a byte[] for Storage to SQL after serializing by calling ms.ToArray().

And don't forget to put your Stream into a using-Statement, to guarantee correct disposal of the allocated resources.

blackforest-tom
  • 438
  • 4
  • 8
2

The easiest way to represent a byte array as a string in C# is with base64 encoding. The below example shows how this would be achieved within your code.

        public void Serialize(Employees emps, String filename)
        {
            //Create the stream to add object into it.
            MemoryStream ms = new MemoryStream();

            //Format the object as Binary

            BinaryFormatter formatter = new BinaryFormatter();
            //It serialize the employee object
            formatter.Serialize(ms, emps);

            // Your employees object serialised and converted to a string.
            string encodedObject = Convert.ToBase64String(ms.ToArray());

            ms.Close();
        }

This creates the string encodedObject. To retrieve the byte array and your serialised object back from the string you will use the below code.

            BinaryFormatter bf = new BinaryFormatter();

            // Decode the string back to a byte array
            byte[] decodedObject = Convert.FromBase64String(encodedObject);

            // Create a memory stream and pass in the decoded byte array as the parameter
            MemoryStream memoryStream = new MemoryStream(decodedObject);

            // Deserialise byte array back to employees object.
            Employees employees = bf.Deserialize(memoryStream);
Ben
  • 757
  • 4
  • 14
  • 2
    Why "ms.Flush(); ms.Close();"? You can directly call "ms.Dispose();" without any problem. – DrkDeveloper Nov 25 '19 at 15:07
  • I just copied and pasted his code then made my changes, you are correct the Flush method does nothing in a memory stream, however the dispose method is also redundant. Calling close will close the stream to reading and writing however the underlying buffers are still available. https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream?view=netcore-3.0 – Ben Nov 25 '19 at 15:18
  • Sorry I just did some reading it turns out Dispose and Close can be used interchangeably. – Ben Nov 25 '19 at 15:31
  • With .Net 5, BinaryFormatter serialization is obsolete and should not be used. – ciyo Feb 27 '21 at 22:08