implements ISerializable
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
[Serializable]
class MyStringData : ISerializable {
public string dataItemOne, dataItemTwo;
public MyStringData() { }
private MyStringData(SerializationInfo si, StreamingContext ctx) {
dataItemOne = si.GetString("First_Item").ToLower();
dataItemTwo = si.GetString("dataItemTwo").ToLower();
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext ctx) {
// Fill up the SerializationInfo object with the formatted data.
info.AddValue("First_Item", dataItemOne.ToUpper());
info.AddValue("dataItemTwo", dataItemTwo.ToUpper());
}
}
[Serializable]
class MoreData {
public string dataItemOne, dataItemTwo;
[OnSerializing]
internal void OnSerializing(StreamingContext context) {
dataItemOne = dataItemOne.ToUpper();
dataItemTwo = dataItemTwo.ToUpper();
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext context) {
dataItemOne = dataItemOne.ToLower();
dataItemTwo = dataItemTwo.ToLower();
}
}
class Program {
static void Main(string[] args) {
MyStringData sd = new MyStringData();
sd.dataItemOne = "some data.";
sd.dataItemTwo = "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);
MoreData md = new MoreData();
md.dataItemOne = "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);
}
}
Related examples in the same category