using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class Person { public string name; public int age; } Person p = new Person(); p.name = "John"; p.age = 25; BinaryFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream("person.bin", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, p); stream.Close();
using System; using System.IO; using System.Runtime.Serialization.Formatters.Soap; [Serializable] class Person { public string name; public int age; } SoapFormatter formatter = new SoapFormatter(); Stream stream = new FileStream("person.soap", FileMode.Open, FileAccess.Read, FileShare.Read); Person p = (Person)formatter.Deserialize(stream); stream.Close(); Console.WriteLine($"Name: {p.name}, Age: {p.age}");In this example, the Person object is deserialized from a Soap-formatted file named "person.soap" using SoapFormatter. The IFormatter interface is part of the System.Runtime.Serialization namespace in the .NET Framework library.