简体   繁体   中英

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,

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!

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.

Serialization contains enough information to pull up the corresponding objects and populate them with date.

For simple objects you can emulate the deserialization process but there is nothing to help you do so. You need to implement everything yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM