static void Main(string[] args) { // then create an instance of that class Person Me = new Person("Josh", 24, "San Jose"); // use a binary formatter and a stream to serialize the instance of the class to a bin file IFormatter writeformatter = new BinaryFormatter(); Stream writestream = new FileStream("SaveMe.bin", FileMode.Create, FileAccess.Write, FileShare.None); writeformatter.Serialize(writestream, Me); writestream.Close(); // to deserialize, use another formatter and stream to load the serialized data to a new class instance IFormatter readformatter = new BinaryFormatter(); Stream readstream = new FileStream("SaveMe.bin", FileMode.Open, FileAccess.Read, FileShare.Read); Person MeAgain = (Person)readformatter.Deserialize(readstream); readstream.Close(); // write the details of the deserialized class instance to the console Console.WriteLine("Name: {0}, Age: {1}, Location: {2}", MeAgain.name, MeAgain.age, MeAgain.location); //Wait for user to acknowledge result Console.WriteLine("Press Enter to terminate..."); Console.Read(); }
static void Main(string[] args) { ArrayList hobbies = new ArrayList(); hobbies.Add("Skiing"); hobbies.Add("Biking"); hobbies.Add("Snowboarding"); Person person = new Person("Brian Pfeil", 28, hobbies); // Binary formatter IFormatter formatter = new BinaryFormatter(); formatter.Serialize(Console.OpenStandardOutput(), person); Console.WriteLine("End Binary Formatter output. Press enter to continue ..."); Console.Read(); // SOAP formatter formatter = new SoapFormatter(); formatter.Serialize(Console.OpenStandardOutput(), person); Console.WriteLine("End Soap Formatter output. Press enter to continue ..."); Console.Read(); //XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person)); //xmlSerializer.Serialize(Console.Out, person); Console.Read(); }
protected override void OnCreate(Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); using (var ms = new MemoryStream()) { XmlSerializer mySerializer = new XmlSerializer (typeof(Person)); var Person = new Person (); Person.Name = "Mathieu"; mySerializer.Serialize (ms, Person); } using (var ms = new MemoryStream()) { XmlSerializer mySerializer = new XmlSerializer (typeof(SomeData)); var someData = new Person (); someData.Name = "Mathieu"; mySerializer.Serialize (ms, someData); ms.Position = 0; Person deserialize = (Person)mySerializer.Deserialize (ms); Button button = FindViewById<Button> (Resource.Id.myButton); button.Text = deserialize.Name; } }
static void Main(string[] args) { //序列化到本地文件 Person person = new Person("Mary") { Age = 30, Phone = "12345678" }; FileStream fileStream = new FileStream("c:\\temp.txt", FileMode.OpenOrCreate, FileAccess.Write); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fileStream, person); fileStream.Close(); //反序列化 fileStream = new FileStream("c:\\temp.txt", FileMode.OpenOrCreate, FileAccess.Read); formatter = new BinaryFormatter(); Person DeserializedPerson = formatter.Deserialize(fileStream) as Person; fileStream.Close(); //输出反序列化的结果 Console.WriteLine(DeserializedPerson.GetName()); Console.WriteLine(DeserializedPerson.Age); Console.WriteLine(DeserializedPerson.Phone); }
static void Main() { var moi = new Person { Nom = "LAMGHARI", Prenom = "Mohammed", Age = 27 }; var serializer = new DataContractSerializer(moi.GetType()); var someRam = new MemoryStream(); serializer.WriteObject(someRam, moi); someRam.Seek(0, SeekOrigin.Begin); Console.WriteLine(Encoding.ASCII.GetString(someRam.GetBuffer())); Console.WriteLine( XElement.Parse( Encoding.ASCII.GetString(someRam.GetBuffer()).Replace("\0", ""))); Console.Read(); }