[Serializable] public class Store { private int sCount; public int stockCount { get { return sCount; } set { sCount = value; } } [NonSerialized] private int temp; private string storeName = "My local store"; public Store() { stockCount = 0; } // Here may follow some code for this class to function }
Store myStore = new Store(); myStore.stockCount = 50; FileStream flStream = new FileStream("MyStore.dat", FileMode.OpenOrCreate, FileAccess.Write); try { BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize(flStream, myStore); } finally { flStream.Close(); tbOutput.Text += Environment.NewLine + "Object 'myStore' serialized!"; tbOutput.Text += Environment.NewLine + "stockCount = 50"; }
Store readStore = new Store(); FileStream flStream = new FileStream("MyStore.dat", FileMode.Open, FileAccess.Read); try { BinaryFormatter binFormatter = new BinaryFormatter(); readStore = (Store)binFormatter.Deserialize(flStream); } finally { flStream.Close(); tbOutput.Text += Environment.NewLine + "Object 'readStore' deserialized!"; tbOutput.Text += Environment.NewLine + "stockCount = " + System.Convert.ToString(readStore.stockCount); }
Store myStore = new Store(); MemoryStream memStream = new MemoryStream(); try { myStore.stockCount = 50; SoapFormatter soapFormatter = new SoapFormatter(); soapFormatter.Serialize(memStream, myStore); byte[] buff = memStream.GetBuffer(); string soapOutput = ""; foreach(byte b in buff) soapOutput += (char)b; tbOutput.Text += Environment.NewLine + "Object 'myStore' serialized to SOAP!"; tbOutput.Text += soapOutput; } finally { memStream.Close(); }
foreach(byte b in buff) soapOutput += (char)b;
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:Store id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ SerializeTest/SerializeTest%2C%20Version%3D1.0.938.186%2C %20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <sCount>50</sCount> <storeName id="ref-3">My local store</storeName> </a1:Store> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
<SOAP-ENV:Envelope xmlns:xsi=...> <SOAP-ENV:Body> <a1:Store id="ref-1" xmlns:a1=...> <sCount>50</sCount> <storeName id="ref-3">My local store</storeName> </a1:Store> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
public void GetObjectData(SerializationInfo serInfo, StreamingContext streamContext) { serInfo.AddValue("stockCount", stockCount); }
public CustomStore(SerializationInfo serInfo, StreamingContext streamContext) { stockCount = serInfo.GetInt32("stockCount"); }
Build Your Own ASP.NET Website Using C# & VB.NET