How to use an XML format to serialize an object of a custom class?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

How to use an XML format to serialize an object of a custom class?

Rating:

The steps for using an XML format to serialize an object of a custom class are as follows:

  1. Create a custom class whose object is to be serialized. An example is given below:

    public class Employees
    {
       public string EmpID;
       public string EmpName;
       public decimal Salary;
    }


  2. Create the object to be serialized and set its public fields and properties. An example is given below:

    Employees emp = new Employees();
    emp.EmpID = "SO1200";
    emp.EmpName = "John Smith";
    emp.Salary = (decimal)10000.00;


  3. Construct an XmlSerializer class object by using the type of the object to be serialized. An example is given below:

    XmlSerializer ObjectSerializer = new XmlSerializer(typeof(Employees));


  4. Determine the serialized object to be written to a StreamWriter object and specify the destination XML stream or a file. An example is given below:

    StreamWriter streamwriter = new StreamWriter("File1.xml");


  5. Call the Serialize method of the XmlSerializer class in order to generate an XML stream or a file. The file will represent the object's public properties and fields. For example:

    ObjectSerializer.Serialize(streamwriter, emp);
    streamwriter.Close();


Rating:



Other articles

Click here to Article home

Microsoft Certification MCSE: MCSA , MCTS, MCDST, MCAD, MCDBA, MCSE Messaging, MCSE Security
JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.
 
HACKER SAFE certified sites prevent over 99.9% of hacker crime.