SERIALIZATION in C#

Drawer DataMatrix in C# SERIALIZATION

CHAPTER 23 SERIALIZATION
ECC200 Printer In C#.NET
Using Barcode drawer for VS .NET Control to generate, create Data Matrix ECC200 image in .NET applications.
www.OnBarcode.com
Data Matrix Decoder In C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
class Listing 12 { static void Main(string[] args) { // create a new Person object Person myPerson = new Person("Adam Freeman", "London"); // create an output stream to the file Stream outputStream = File.OpenWrite("person.soap"); // create a new binary formatter IFormatter serializer = new SoapFormatter(); // serialize the object serializer.Serialize(outputStream, myPerson); // close the stream outputStream.Close(); // create a formatter IFormatter deserializer = new SoapFormatter(); // open the stream to the file Stream inputStream = File.OpenRead("person.soap"); // deserialize the object Person deserializedPerson = (Person)deserializer.Deserialize(inputStream); // print out the value of the fields of the deserialized object Console.WriteLine("Deserialized name: {0}", deserializedPerson.Name); Console.WriteLine("Deserialized city: {0}", deserializedPerson.City); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } The only change here is that you create a SoapFormatter object instead of a BinaryFormatter object. Both classes implement the IFormatter interface, which means that you can switch between them with very few code changes. However, as the following sections explain, there are some important differences between the capabilities of the SoapFormatter and BinaryFormatter class.
EAN / UCC - 13 Drawer In C#
Using Barcode generation for .NET Control to generate, create UCC - 12 image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing Data Matrix 2d Barcode In Visual C#
Using Barcode maker for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Version Tolerance
Printing 2D Barcode In C#
Using Barcode printer for .NET Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
PDF-417 2d Barcode Encoder In Visual C#
Using Barcode generation for .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
The SoapFormatter class doesn t support the default version tolerance that BinaryFormatter uses. An exception will be thrown if a new field has been added to a class since the object was serialized and the field hasn t been annotated with the OptionalField attribute.
Create Linear 1D Barcode In C#.NET
Using Barcode generation for .NET Control to generate, create 1D Barcode image in VS .NET applications.
www.OnBarcode.com
Identcode Encoder In C#
Using Barcode generation for .NET framework Control to generate, create Identcode image in .NET applications.
www.OnBarcode.com
CHAPTER 23 SERIALIZATION
Data Matrix Printer In None
Using Barcode generation for Font Control to generate, create Data Matrix image in Font applications.
www.OnBarcode.com
Decode Data Matrix In VS .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Serializing Generic Objects
Barcode Decoder In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Generating GS1 128 In None
Using Barcode creation for Software Control to generate, create GS1 128 image in Software applications.
www.OnBarcode.com
The SoapFormatter class does not support serializing generic objects. This can be a critical limitation, most frequently if you want to serialize collections as demonstrated by Listing 23-6.
Creating Barcode In .NET Framework
Using Barcode encoder for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Barcode Drawer In Java
Using Barcode drawer for BIRT reports Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Using XML Serialization
Generate PDF-417 2d Barcode In None
Using Barcode generation for Office Excel Control to generate, create PDF417 image in Microsoft Excel applications.
www.OnBarcode.com
Drawing Barcode In Java
Using Barcode creation for Eclipse BIRT Control to generate, create Barcode image in Eclipse BIRT applications.
www.OnBarcode.com
The third serialization option is known as XML Serialization and is performed using the System.Xml.Serialization.XmlSerializer class. The XmlSerializer produces XML, but it only includes the values of an object s public fields and properties in the serialization data. The values of private fields and properties are omitted. You do not need to apply any attributes to make a class serializable by XmlSerializer, but the class must be public and must have a parameterless constructor. Listing 23-13 demonstrates using the XmlSerializer class. Listing 23-13. Serializing Using the XmlSerializer Class using System; using System.Xml.Serialization; using System.IO; public class Person { private string name; private string city; public Person() { // do nothing } public Person(String nameParam, String cityParam) { name = nameParam; city = cityParam; } public String Name { get { return name; } set { name = value; } } public String City { get { return city; } set { city = value; } } } class Listing 13 { static void Main(string[] args) { // create a new Person object
Encoding Barcode In Visual Studio .NET
Using Barcode generation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Encoding EAN13 In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create EAN 13 image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 23 SERIALIZATION
Creating Barcode In VB.NET
Using Barcode creation for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
Encoding PDF 417 In None
Using Barcode drawer for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Person myPerson = new Person("Adam Freeman", "London"); // open the stream to the file we want to store the data in Stream outputStream = File.OpenWrite("person.xml"); // create the XMLSerializer XmlSerializer serializer = new XmlSerializer(typeof(Person)); // serialize the object serializer.Serialize(outputStream, myPerson); // close the stream to the file outputStream.Close(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Listing 23-13 contains a Person class that has three changes from previous examples: the class is public, it contains a parameterless constructor, and the Serializable attribute has not been applied. When creating an XmlSerializer object, you must pass the type of the object that you are going to serialize. You can get this using the typeof keyword or by calling the GetType method on the object itself, like this:
XmlSerializer serializer = new XmlSerializer(myPerson.GetType());
Once you have created an XmlSerializer object, you can call the Serialize method, passing in the Stream you want the XML written to and the object to serialize. Here is the content of the file created by compiling and running Listing 23-13: < xml version="1.0" > <Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Name>Adam Freeman</Name> <City>London</City> </Person> The process for deserializing is very similar, except that you call the Deserialize method, passing the Stream from which the serialized data is to be read. Here is an example: // open a stream so we can read the file Stream inputStream = File.OpenRead("person.xml"); // create an XmlSerializer object XmlSerializer deserializer = new XmlSerializer(typeof(Person)); // deserialize the object from the stream Person myDeserializedPerson = (Person)deserializer.Deserialize(inputStream); // print out details from the deserialized person object Console.WriteLine("Name: {0}", myDeserializedPerson.Name); Console.WriteLine("City: {0}", myDeserializedPerson.City);
Copyright © OnBarcode.com . All rights reserved.