CHAPTER 17 (Web site designers) 552 UNDERSTANDING OBJECT SERIALIZATION Serializing
Wednesday, March 12th, 2008CHAPTER 17 552 UNDERSTANDING OBJECT SERIALIZATION Serializing Objects Using the SoapFormatter Your next choice of formatter is the SoapFormatter type. The SoapFormatter will persist an object graph into a SOAP message, which makes this formatter a solid choice when you wish to distribute objects remotely using the HTTP protocol. If you are unfamiliar with the SOAP specification, don t sweat the details right now. In a nutshell, SOAP defines a standard process in which methods may be invoked in a platform- and OS-neutral manner (we ll examine SOAP in a bit more detail in the final chapter of this book during a discussion of XML web services). Assuming you have set a reference to the System.Runtime.Serialization.Formatters.Soap.dll assembly, you could persist and retrieve a JamesBondCar as a SOAP message simply by replacing each occurrence of BinaryFormatter with SoapFormatter. Consider the following code, which serializes an object to a local file named CarData.soap: using System.Runtime.Serialization.Formatters.Soap; … static void Main(string[] args) { … // Save object to a file named CarData.soap in SOAP format. SoapFormatter soapFormat = new SoapFormatter(); fStream = new FileStream(”CarData.soap”, FileMode.Create, FileAccess.Write, FileShare.None); soapFormat.Serialize(fStream, jbc); fStream.Close(); Console.ReadLine(); } As before, simply use Serialize() and Deserialize() to move the object graph in and out of the stream. If you open the resulting *.soap file, you can locate the XML elements that mark the stateful values of the current JamesBondCar as well as the relationship between the objects in the graph via the #ref tokens. Consider the following end result (XML namespaces snipped for brevity):
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.