c# code 39 checksum Classes Involved in the Serialization Process in C#

Encoder Code-39 in C# Classes Involved in the Serialization Process

Classes Involved in the Serialization Process
ANSI/AIM Code 39 Printer In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create Code 39 image in Visual Studio .NET applications.
www.OnBarcode.com
Decoding ANSI/AIM Code 39 In C#.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
There are three core classes that are used to perform serialization in binary, XML, and SOAP formats, respectively: The BinaryFormatter class serializes objects in binary format. It resides in the System.Runtime.Serialization.Formatters.Binary namespace. The XmlSerializer class serializes objects in XML format. It resides in the System.Xml. Serialization namespace. The System.Xml.Serialization name- space physically resides in the System.Xml.dll assembly. The SoapFormatter class serializes objects in SOAP format. It resides in the System. Runtime.Serialization.Formatters.Soap namespace. The System.Runtime.Serialization. Formatters.Soap namespace physically resides in the System.Runtime.Serialization. Formatters.Soap.dll assembly.
QR Code JIS X 0510 Drawer In C#
Using Barcode creator for .NET framework Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
Data Matrix 2d Barcode Maker In C#.NET
Using Barcode creation for .NET framework Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZA TION
Code 128A Creation In C#
Using Barcode generation for .NET Control to generate, create Code128 image in .NET applications.
www.OnBarcode.com
Code 3 Of 9 Printer In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create Code39 image in .NET applications.
www.OnBarcode.com
Serializing and Deserializing Objects by Using XML Format
Create PDF 417 In Visual C#.NET
Using Barcode printer for VS .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
C 2 Of 5 Encoder In Visual C#.NET
Using Barcode maker for .NET Control to generate, create Industrial 2 of 5 image in Visual Studio .NET applications.
www.OnBarcode.com
Now that you have a basic understanding of what serialization is, let s delve straight into XML serialization. You will be building an application that illustrates the serialization and deserialization process by using the XmlSerializer class. The application user interface is shown in Figure 8-1.
Code 3 Of 9 Reader In Visual Studio .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Encode ANSI/AIM Code 39 In Objective-C
Using Barcode generation for iPad Control to generate, create Code 3/9 image in iPad applications.
www.OnBarcode.com
Figure 8-1. Application for illustrating XML serialization The application consists of a class called Employee with five public properties: EmployeeID, FirstName, LastName, HomePhone, and Notes. There are five text boxes that accept values for these properties. The two buttons, Serialize and Deserialize, do the job of serializing and deserializing the Employee object, respectively. The check box determines whether the serialized XML document will be opened in a browser for viewing. Before you can use the XmlSerializer class, you should create the Employee class as shown in Listing 8-1. Listing 8-1. The Employee Class public class Employee { private int intID; private string strFName; private string strLName; private string strHPhone; private string strNotes;
Barcode Encoder In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Draw Barcode In Objective-C
Using Barcode creation for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZATION
PDF 417 Generator In Java
Using Barcode generation for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
Drawing Code 3 Of 9 In Objective-C
Using Barcode encoder for iPad Control to generate, create Code 39 image in iPad applications.
www.OnBarcode.com
public int EmployeeID { get { return intID; } set { intID = value; } } public string FirstName { get { return strFName; } set { strFName = value; } } public string LastName { get { return strLName; } set { strLName = value; } } public string HomePhone { get { return strHPhone; } set
ANSI/AIM Code 128 Maker In None
Using Barcode maker for Excel Control to generate, create Code 128 image in Office Excel applications.
www.OnBarcode.com
Create Linear 1D Barcode In VS .NET
Using Barcode creator for ASP.NET Control to generate, create Linear image in ASP.NET applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZA TION
Scan PDF 417 In .NET Framework
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Drawing GTIN - 13 In Java
Using Barcode encoder for Java Control to generate, create EAN13 image in Java applications.
www.OnBarcode.com
{ strHPhone = value; } } public string Notes { get { return strNotes; } set { strNotes = value; } } } The class declares five private variables for storing various pieces of information about an employee. These five private variables are exposed to the external world via five public properties (EmployeeID, FirstName, LastName, HomePhone, and Notes). The Click event handler of the Serialize button contains the code shown in Listing 8-2. Listing 8-2. Serializing Objects in XML Format private void button1_Click(object sender, EventArgs e) { Employee emp = new Employee(); emp.EmployeeID = int.Parse(textBox1.Text); emp.FirstName = textBox2.Text; emp.LastName = textBox3.Text; emp.HomePhone = textBox4.Text; emp.Notes = textBox5.Text; FileStream stream = new FileStream(Application.StartupPath + @"\employee.xml", FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Employee)); serializer.Serialize(stream, emp); stream.Close(); if (checkBox1.Checked) { Process.Start(Application.StartupPath + @"\employee.xml"); } }
Code 128A Recognizer In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
DataMatrix Generation In Java
Using Barcode maker for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZATION
The code creates an instance of the Employee class. It then assigns values from various text boxes to the corresponding properties of the Employee class. A FileStream is then created for writing to a physical disk file (Employee.xml). This stream is used while actually serializing the object. Then the code creates an object of the XmlSerializer class. As mentioned previously, the XmlSerializer class allows you to serialize data in XML format. There are several overloads of the XmlSerializer constructor, and the code uses the one that accepts the type of class whose objects are to be serialized. The type information about the Employee class is obtained by using the typeof keyword. The Serialize() method of XmlSerializer serializes an object to a specified stream, TextWriter, or XmlWriter. Because our example uses a FileStream to serialize the Employee object, after serialization is complete, the stream is closed. Finally, the serialized data residing in the XML file is displayed in a browser by using the Start() method of the Process class. The Click event handler of the Deserialize button contains the code shown in Listing 8-3. Listing 8-3. Deserializing by Using the XmlSerializer Class private void button2_Click(object sender, EventArgs e) { Employee emp; FileStream stream = new FileStream(Application.StartupPath + @"\employee.xml", FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(Employee)); emp=(Employee)serializer.Deserialize(stream); stream.Close(); textBox1.Text = emp.EmployeeID.ToString(); textBox2.Text = emp.FirstName; textBox3.Text = emp.LastName; textBox4.Text = emp.HomePhone; textBox5.Text = emp.Notes; } The code declares a variable of type Employee. Then it creates a FileStream pointing to the same file that was created during the serialization process. Note that this time the file is opened in Open mode and not in Create mode. Then an object of XmlSerializer is created as before. The Deserialize() method of the XmlSerializer class accepts a Stream, a TextReader, or an XmlReader from which the object is to be read for deserialization. It then returns the deserialized object. The deserialized data is always returned as a generic-type object and needs to be cast to the Employee type. Then various property values of the deserialized object are assigned to respective text boxes. To test the application, you run it, enter some values in the text boxes, and click the Serialize button. Figure 8-2 shows a sample XML document obtained by running the preceding application.
Copyright © OnBarcode.com . All rights reserved.