code 39 c# class XML S ERIA LIZATION in C#.NET

Making Code 39 Extended in C#.NET XML S ERIA LIZATION

CHAPTER 8 XML S ERIA LIZATION
ANSI/AIM Code 39 Printer In Visual C#
Using Barcode drawer for Visual Studio .NET Control to generate, create Code-39 image in .NET framework applications.
www.OnBarcode.com
Scan Code 39 In C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
The code is essentially the same as in the preceding examples. However, it sets the newly added properties to corresponding values from the text boxes and combo box. Notice how the complex property Address is set. Also, notice how comma-separated emails entered in the email text box are converted into a string array by using the Split() method. After the Employee object is serialized by calling Serialize(), the serialized XML document looks like the one shown in Figure 8-5.
Drawing UPC - 13 In C#
Using Barcode generator for .NET Control to generate, create European Article Number 13 image in VS .NET applications.
www.OnBarcode.com
Create PDF 417 In C#.NET
Using Barcode printer for .NET framework Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
Figure 8-5. Serialized XML for complex types Examine the serialized XML data carefully. The address is represented by the <Address> node, the name of which is derived from the Address property of the Employee class. The <Address> node has five child nodes: <Street>, <City>, <State>, <Country>, and <PostalCode>. Their names are derived from the respective properties of the Address class. The <Type> element represents the Type property of the Employee class. The enumeration value, Permanent, is stored in the XML markup. Finally, the <Emails> node represents the Emails property, and its child nodes are nothing but individual array elements. Because the emails are stored in a string array, the individual values are enclosed in <string></string> elements. Listing 8-10 shows the code in the Click event of the Deserialize button.
Barcode Printer In C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Paint 2D In C#
Using Barcode generator for VS .NET Control to generate, create Matrix 2D Barcode image in .NET framework applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZA TION
Printing Data Matrix In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix image in VS .NET applications.
www.OnBarcode.com
Making UPC-E Supplement 5 In C#
Using Barcode printer for VS .NET Control to generate, create GTIN - 12 image in VS .NET applications.
www.OnBarcode.com
Listing 8-10. Deserializing Complex Types 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; comboBox1.SelectedIndex = (emp.Type == EmployeeType.Permanent 0:1); textBox6.Text=emp.Address.Street; textBox7.Text=emp.Address.City; textBox8.Text=emp.Address.State; textBox9.Text=emp.Address.Country; textBox10.Text=emp.Address.PostalCode; textBox11.Text = string.Join(",", emp.Emails); stream.Close(); } The code is very much the same as in previous examples. It deserializes the previously serialized Employee object by using the XmlSerializer class. The property values are then assigned to various controls on the form. Notice how the Emails property is converted into a comma-separated string by using the Join() method of the string class. The following points are worth noting when serializing complex types: To serialize and deserialize enumerated values, the application that serializes the object and the application that deserializes it must define the same enumeration under consideration. While serializing object properties, all the public members of the object are serialized. The member names are assigned to the child elements in the resultant XML. During the deserialization process, XmlSerializer instantiates the main class (Employee) as well as all the subclasses (Address) and assigns values to the respective properties. While serializing arrays, an XML element represents the array. The individual array elements form the child element of this element. The individual array elements are enclosed in an element depending on the data type of the array. While deserializing, XmlSerializer creates an array with the same number of elements as the serialized elements. It then assigns the array element values accordingly.
Decode Code-39 In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Painting Code 39 In None
Using Barcode printer for Font Control to generate, create Code 39 Extended image in Font applications.
www.OnBarcode.com
CHAPTER 8 XML S ERIA LIZATION
Reading UCC.EAN - 128 In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
DataMatrix Maker In VB.NET
Using Barcode creation for .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
www.OnBarcode.com
Serialization and Inheritance
Painting Code 39 In Java
Using Barcode generator for Android Control to generate, create ANSI/AIM Code 39 image in Android applications.
www.OnBarcode.com
Draw UPC Code In Java
Using Barcode printer for Android Control to generate, create GS1 - 12 image in Android applications.
www.OnBarcode.com
Serialization is not limited to simple and complex types. It is equally applicable to inherited classes. Assume that you have a class called Manager that inherits from our Employee class. Now when you serialize Manager, all the public properties of the Employee base class and Manager are serialized. This is also true in the case of a long chain of inheritance. To demonstrate how inherited classes are serialized, we need to add a class called Manager to our application. The Manager class inherits from the Employee class (see Listing 8-1) and extends it by adding an integer property NoOfSubordinates. The Manager class is shown in Listing 8-11. Listing 8-11. The Manager Class public class Manager:Employee { private int intNoOfSubordinates; public int NoOfSubordinates { get { return intNoOfSubordinates; } set { intNoOfSubordinates = value; } } } The code creates a class named Manager that inherits from the Employee class. It then adds a private integer variable to store the number of subordinates of a manager. The variable is exposed to the external world via a public property, NoOfSubordinates. To accommodate the additional property, the user interface of the application changes as shown in Figure 8-6. The application is almost the same as in Figure 8-1, but there is an extra text box for accepting the number of subordinates of the manager. Listing 8-12 shows the Click event handler of the Serialize button.
Painting Code39 In None
Using Barcode drawer for Software Control to generate, create ANSI/AIM Code 39 image in Software applications.
www.OnBarcode.com
DataMatrix Recognizer In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Drawing Barcode In Java
Using Barcode encoder for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Read Barcode In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Generate Universal Product Code Version A In Java
Using Barcode generator for BIRT reports Control to generate, create Universal Product Code version A image in Eclipse BIRT applications.
www.OnBarcode.com
Generating Barcode In None
Using Barcode generation for Word Control to generate, create Barcode image in Microsoft Word applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.