static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Serialization *****"); // StringData : ISerializable StringData myData = new StringData(); // When saving serialized data in file // ISerializable.GetObjectData() is invoked SoapFormatter soapFormat = new SoapFormatter(); using (Stream fStream = new FileStream("MyData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, myData); } Console.WriteLine("Custom serialize for StringData completed"); MoreData myDataForMoreData = new MoreData(); // Save to a local file in SOAP format. SoapFormatter soapFormatForMoreData = new SoapFormatter(); using (Stream fStream = new FileStream("MyDataForMoreData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormatForMoreData.Serialize(fStream, myDataForMoreData); } Console.WriteLine("Custom serialize for MoreData completed"); }
static void Main(string[] args) { Console.WriteLine("*********Fun with Custom Serialization ***********"); StringData data = new StringData(); SoapFormatter soapFormatter = new SoapFormatter(); using (FileStream fStream = new FileStream("StringData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormatter.Serialize(fStream, data); } //序列化 MoreData data1 = new MoreData(); BinaryFormatter binaryFormatter = new BinaryFormatter(); using (FileStream fStream = new FileStream("MoreData.txt", FileMode.Create, FileAccess.Write, FileShare.None)) { binaryFormatter.Serialize(fStream, data1); } //反序列化 using (FileStream fStream = File.OpenRead("MoreData.txt")) { MoreData data2 = (MoreData)binaryFormatter.Deserialize(fStream); Console.WriteLine(data2.ToString()); } Console.ReadLine(); }
private static void CustomizeByAttributes(string file) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"=> Serialize {file} as Soap Using Attribute"); MoreData myData = new MoreData(); SoapFormatter soap = new SoapFormatter(); using (Stream s = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)) { soap.Serialize(s, myData); } }
static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Serialization *****"); // Recall that this type implements attributes MoreData myData = new MoreData(); // Save to a local file in Soap format. SoapFormatter soapFormat = new SoapFormatter(); using (Stream fStream = new FileStream("MyData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, myData); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Serialization *****\n"); #region Serialize MyStringData // Persist MyStringData in SOAP format. MyStringData sd = new MyStringData(); sd.dataItemOne = "This is some data."; sd.dataItemTwo = "Here is some more data"; Stream s = new FileStream("MyData.soap", FileMode.Create, FileAccess.Write, FileShare.None); SoapFormatter sf = new SoapFormatter(); sf.Serialize(s, sd); s.Close(); s = File.OpenRead("MyData.soap"); sd = (MyStringData)sf.Deserialize(s); Console.WriteLine("Item 1: {0}", sd.dataItemOne); Console.WriteLine("Item 2: {0}", sd.dataItemTwo); #endregion #region Serialize MoreData // Persist MoreData in SOAP format. MoreData md = new MoreData(); md.dataItemOne = "Testing 1, 2, 3."; md.dataItemTwo = "One more test..."; s = new FileStream("MoreData.soap", FileMode.Create, FileAccess.Write, FileShare.None); sf = new SoapFormatter(); sf.Serialize(s, md); s.Close(); s = File.OpenRead("MoreData.soap"); md = (MoreData)sf.Deserialize(s); Console.WriteLine("Item 1: {0}", md.dataItemOne); Console.WriteLine("Item 2: {0}", md.dataItemTwo); Console.ReadLine(); #endregion }
static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Seralization *****"); // 这个类型实现了ISerializable StringData myData = new StringData(); // 以SOAP格式保存到本地文件中 SoapFormatter soapFormat = new SoapFormatter(); using (Stream fStream = new FileStream("MyData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, myData); } MoreData moreData = new MoreData(); using (Stream fStream = new FileStream("MoreData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, moreData); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Custom Serialization *****"); // Recall, this type implements ISerializable. // Uncomment to persist. // StringData myData = new StringData(); // This type customizes the serialization process // using .NET attributes. MoreData myData = new MoreData(); // Save to a local file in SOAP format. SoapFormatter soapFormat = new SoapFormatter(); using (Stream fStream = new FileStream("MyData.soap", FileMode.Create, FileAccess.Write, FileShare.None)) { soapFormat.Serialize(fStream, myData); } Console.WriteLine("Done! Check app directory for output"); Console.ReadLine(); }