Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we read an object serialized in .NET in a linux machine?

I need to transfer some files written in serialized form as files in a windows machine (C#.NET serialization) to a linux machine. How can I achieve this? I need to use perl/Java/bash in linux side preferably.

Edit: To be clearer, files are text files.. but binary serialized in .NET. In linux side, I need to use Perl/Java/Bash to de-serialize and read these files. I have the constraint that the .NET side code cannot be touched.. Anything I do has to be on the linux side..

Thanks,

like image 923
Hari Menon Avatar asked Dec 17 '10 14:12

Hari Menon


People also ask

Is object serializable C#?

Serialization in C# is the process of converting an object into a stream of bytes to store the object to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

Why do we need to serialize an object in C#?

Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.

What is .NET serialization?

Serialization is the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred.

Where do we use serialization in C#?

In C#, serialization is the process of converting object into byte stream so that it can be saved to memory, file or database. The reverse process of serialization is called deserialization. Serialization is internally used in remote applications.


2 Answers

You can deserialise .NET-serialised data on Linux if you have a .NET CLI implementation, such as Mono or DotGNU. This way you would be able to write a C# wrapper to handle the deserialisation then, as Brian stated above, reserialise using XML if you want to use the data in a non .NET application.

For .NET, the necessary namespaces and classes are:

BinaryFormatter and FileStream classes:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.IO.FileStream

To deserialise, create an instance of the BinaryFormatter and FileStream classes, loading the serialised data into the FileStream. Then call Deserialize on the BinaryFormatter and cast into the necessary data type (I've called it TheClass below):

BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.OpenRead(@"InsertFileName");
TheClass classInstance = (TheClass)formatter.Deserialize(file);
file.Close();

XML serialisation using raw XML or SOAP is more interoperable with non .NET apps. SOAP serialisation is available using the SoapFormatter class:

System.Runtime.Serialization.Formatters.Soap.SoapFormatter

Serialisation is performed by creating FileStream and SoapFormatter instances and calling the Soapformatter Serialize method. To serialise the classInstance example above:

FileStream file = File.Create(@"InsertFileName");
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(file, classInstance);
file.Close();

Raw XML serialisation is highly customisable but works slightly differently. The XMLSerializer class is used for this purpose:

System.Xml.Serialization.XmlSerializer

To serialize TheClass using XML serialisation, you will need instances of XmlSerializer and StreamWriter (in System.IO):

XmlSerializer serializer = new XmlSerializer(typeof(TheClass));
StreamWriter xmlFile = new StreamWriter(@"InsertFileName");
serializer.Serialize(xmlFile, classInstance);
xmlFile.Close();

Once in XML, either raw or SOAP, other languages such as Java should have little difficulty reading them. For more info on XML serialisation, see this page on MSDN.

To work with .NET on Linux, the Mono Project have created an IDE called MonoDevelop which works in a similar way to Visual Studio on Windows.

I hope this infornmation is useful!

like image 193
BWHazel Avatar answered Nov 14 '22 23:11

BWHazel


Even though you can't touch the .Net code, you could probably still solve this problem by writing a simple .Net program which takes as input a serialized object and gives as output a reserialized object (using a serialization that is easier to read, such as XML). If that is not practical, you will be experiencing pain. I'm not aware of a Linux wrapper that can read windows .Net Binary Serialized files, though it is possible that Mono knows how to do it.

If you want to do it yourself, you may find Lluis Sanchez Gual's page to be a helpful start at documenting how it works, though it's an old page.

like image 28
Brian Avatar answered Nov 14 '22 22:11

Brian