Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C# .Net : XML Serialization of simple class object containing properties

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 301
    Comment on it

    C# .Net : XML Serialization of simple class object containing multiple properties

     

    The process of converting an object into stream is Serialization. This generated stream then can be used either for saving it in a file or for transporting/sending it into communication channel. XML Serialization of simple class object performs serialization of a class object into xml. XML Serialization require XmlSerializer class which is derived from "System.Xml.Serialization".

     

    Example to demonstrate XML Serialization of simple class object containing properties :-

     

    namespace DemoApplication
    {
        public class XmlSerialize
        {
            [XmlElement("EmployeeName")]
            public string Employee_name { get; set; }
            [XmlElement("EmployeeDepartment")]
            public string Employee_Department { get; set; }
        }
        public class Serialization
        {
            public static void Main(String[] arg)
            {
                // XmlSerialize class instance is created
                XmlSerialize objSerialize = new XmlSerialize();
    
                // Initializing properties of XmlSerialize class
                objSerialize.Employee_name = "Deepak Rana";
                objSerialize.Employee_Department = "Mechanical";
    
                // XmlSerializer class instance of type XmlSerialize is created
                XmlSerializer serializer = new XmlSerializer(typeof(XmlSerialize));
    
                //StreamWriter object assigned to TextWriter instance
                TextWriter textWriter = new StreamWriter(@"D:\BasicSerialization.xml");
    
                //Serialization of objSerialize is performed and saved in physical location
                serializer.Serialize(textWriter, objSerialize);
                textWriter.Close();
            }
        }
    }

     

    Steps used in above program :-

     

    • Define class "XmlSerialize" with some properties, within main object of this class is created which is the object to be serialized.

     

    • An object of XmlSerializer class is created of type "XmlSerialize",now this object will be used to serialize.

     

    • An instance of TextWriter is created which is assigned to StreamWriter object for saving file in some physical location. TextWriter is an abstract class.

     

    • “Serialize” method of xmlSerializer class is called which performs xml serialization of "XmlSerializer" object of type "XmlSerialize".

     

    • Finally TextWriter is closed.

     

    When above program is run an xml file named "BasicSerialization.xml" is created in mentioned physical location. The contents of this file is as follows :-

     

    <?xml version="1.0" encoding="UTF-8"?>
    -<XmlSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <EmployeeName>Deepak Rana</EmployeeName>
    <EmployeeDepartment>Mechanical</EmployeeDepartment>
    </XmlSerialize>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: