XML SERIALIZATION IN C#.NET

SERIALIZATION
Serialization is the process of turning an object in memory into a stream of byte so you can do stuff like store it on disk or send it over the network.
A C# object can be serialized to XML as specified-
⦁ To use Serialization in xml we need XmlSerializer class.
⦁ This class is derived from System.Xml.Serialization.
⦁ Only public objects can be serialized in XML serialization

# How to do XML Serialization?
*There are many ways in C# to serialize an object to xml.
1) XML Serialization using simple class object.
2) XML Serialization using a Class object containing many properties.
3) XML Serialization using XMLElement.
4) XML Serialization using array of Objects.
1) XML Serialization using simple class object
Serialize the object into XML.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.IO; namespace XmlSerSimpleObj { public class SimpleXml { public String name = "XmlSimpleObject"; static void Main(string[] args) { SimpleXml serializeObject = new SimpleXml(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(SimpleXml)); // Create an instance of stream writer. TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml"); // Serialize the instance of XmlSerSimpleObj. xmlSerializer.Serialize(txtWriter, serializeObject); // Close the stream writer Console.WriteLine("xml is serialized"); txtWriter.Close(); Console.ReadLine(); } } }

2)XML Serialization using a Class object containing many properties:
Serialize the object into XML for many properties define within the public class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace XmlobjSerialization
{
    public class Bike
    {
        public string Name { get; set; }
        public string Price { get; set; }
        public string Color { get; set; }
        private static void Main(string[] args)
        {
           //Create an instance of Bike class.
            Bike bike = new Bike();
            bike.Name = "Splendor-Plus";
            bike.Price = "50000";
            bike.Color = "Black";
            // Create and instance of XmlSerializer class. 
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike));
            // Create an instance of stream writer.
            TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml");
            // Serialize the instance of XmlobjSerialization.
            xmlSerializer.Serialize(txtWriter, bike);
            // Close the stream writer.
            txtWriter.Close();
        }
    }
}

3)XML Serialization using XMLElement:
Control the name of properties using XMLElement in XML serialization.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace xmlserusingXElement
{
    public class Bike
{
  [XmlElement("Bike name")]
  public string Name { get; set; }
  [XmlElement("Bike price")]
  public string Price { get; set; }
  [XmlElement("Bike  Brand")]
  public string Brand { get; set; }
        static void Main(string[] args)
        {
            Bike bike = new Bike();
            bike.Name = "Splendor";
            bike.Price = "50000";
            bike.Brand = "Honda";
            // Create and instance of XmlSerializer class.
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike));
            // Create an instance of stream writer.
            TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml");
            // Serialize the instance of xmlserusingXElement.
            xmlSerializer.Serialize(txtWriter, bike);
            // Close the stream writer
            txtWriter.Close();
        }
    }
}

4)XML Serialization using array of Objects:
Serialize the array of objects into XML.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace XmlSerUsingOjcOfArray
{
    class Bike
    {
        public string Name { get; set; }
        public string Price { get; set; }
        private void Main(string[] args)
        {
            //Create first instance of Bike class.
            Bike bike1 = new Bike();
            bike1.Name = "Honda-City";
            bike1.Price = "52190";
            //Create second instance of Bike class.
            Bike bike2 = new Bike();
            bike2.Name = "Yamaha";
            bike2.Price = "50000";
            // Create an array of Bike
            Bike[] bikes = new Bike[2];
            bikes[0] = bike1;
            bikes[1] = bike2;
            // Create and instance of XmlSerializer class. 
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike[]));
            // Create an instance of stream writer.
            TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml");
            // Serialize the instance of  XmlSerUsingOjcOfArray.
            xmlSerializer.Serialize(txtWriter, bikes);
            // Close the stream writer
            txtWriter.Close();
        }
    }
}