Forums - C# Corner

Forum guidelines
Pooja Singh

Pooja Singh

  • 1.3k
  • 144
  • 11.1k

Serialization in C# and .NET

Jul 9 2016 8:43 AM
<em><strong><span style="color: #ff0000;">SERIALIZATION</span></strong><br /> 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.</em><em> <p>A C# object can be serialized to XML as specified-<br /> ? To use Serialization in xml we need XmlSerializer class.<br /> ? This class is derived from System.Xml.Serialization.<br /> ? Only public objects can be serialized in XML serialization</p> </em><div><em><strong> <span style="color: #00ff00;"># How to do XML Serialization?</span></strong></em></div> <div><em><strong><span style="color: #ff0000;">*There are many ways in C# to serialize an object to xml.</span></strong><br /> 1) XML Serialization using simple class object.<br /> 2) XML Serialization using a Class object containing many properties.<br /> 3) XML Serialization using XMLElement.<br /> 4) XML Serialization using array of Objects.</em></div> <div> <em><strong>1) <span style="color: #993366;">XML Serialization using simple class object</span><br /> </strong>Serialize the object into XML.<br /><code></code></em><code><pre><div>&nbsp;</div><div><em>using System;</em></div><div><em>using System.Collections.Generic;</em></div><div><em>using System.Linq;</em></div><div><em>using System.Text;</em></div><div><em>using System.Xml.Serialization;</em></div><div><em>using System.IO;</em></div><div><em>namespace XmlSerSimpleObj</em></div><div><em>{ </em></div><div><em>public class SimpleXml </em></div><div><em> { </em></div><div><em> public String name = "XmlSimpleObject"; </em></div><div> <em>static void Main(string[] args) </em></div><div><em>{ </em></div><div><em> SimpleXml serializeObject = new SimpleXml(); </em></div><div><em> XmlSerializer xmlSerializer = new XmlSerializer(typeof(SimpleXml)); </em></div><div><em> </em></div><div><em><span style="color: #008000;">// Create an instance of stream writer.</span> </em></div><div><em> TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml"); </em></div><div><em> <span style="color: #008000;">// Serialize the instance of XmlSerSimpleObj.</span> </em></div><div><em>xmlSerializer.Serialize(txtWriter, serializeObject); </em></div><div><em> <span style="color: #008000;">// Close the stream writer </span> </em></div><div><em> Console.WriteLine("xml is serialized"); </em></div><div><em> txtWriter.Close(); </em></div><div><em>Console.ReadLine(); </em></div><div><em>} </em></div><div><em> } </em></div><div><em>} </em></div></pre> </code><p><code></code> </p></div> <div><em><strong>2)<span style="color: #993366;">XML Serialization using a Class object containing many properties:</span><br /> </strong>Serialize the object into XML for many properties define within the public class.<br /> <code> </code></em><code><pre><em>using System; <br />using System.Collections.Generic;<br />&nbsp;using System.Linq;<br />&nbsp;using System.Text;<br />&nbsp;using System.Xml.Serialization;<br />&nbsp;using System.IO;<br />&nbsp;namespace XmlobjSerialization<br />&nbsp;{ <br /> public class Bike <br /> { <br /> public string Name { get; set; } <br /> public string Price { get; set; } <br /> public string Color { get; set; } <br /> private static void Main(string[] args) <br /> { <br /> <span style="color: #008000;"> //Create an instance of Bike class.</span> <br /> Bike bike = new Bike(); <br /> bike.Name = "Splendor-Plus"; <br /> bike.Price = "50000"; <br /> bike.Color = "Black"; <br /> <span style="color: #008000;">// Create and instance of XmlSerializer class. </span> <br /> XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike)); <br /> <span style="color: #008000;">// Create an instance of stream writer. </span> <br /> TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml"); <br /> <span style="color: #008000;">// Serialize the instance of XmlobjSerialization. </span><br /> xmlSerializer.Serialize(txtWriter, bike); <br /> <span style="color: #008000;">// Close the stream writer. </span><br /> txtWriter.Close(); <br />} <br />}<br /> <br />&nbsp;}</em> </pre> </code><p><code></code> </p></div> <div><em><strong>3<span style="color: #993366;">)XML Serialization using XMLElement:</span><br /> </strong>Control the name of properties using XMLElement in XML serialization.<br /> <code> <pre>using System;<br />using System.Collections.Generic; <br />using System.Linq;<br />&nbsp;using System.Text;<br />&nbsp;using System.Xml.Serialization;<br />&nbsp;using System.IO;<br />&nbsp;namespace xmlserusingXElement<br />&nbsp;{ <br />public class Bike <br />{ <br />[XmlElement("Bike name")] <br />public string Name { get; set; } <br />[XmlElement("Bike price")] <br />public string Price { get; set; } <br /> [XmlElement("Bike Brand")] <br />public string Brand { get; set; } <br /> static void Main(string[] args) <br /> { <br /> Bike bike = new Bike(); <br />bike.Name = "Splendor"; <br /> bike.Price = "50000"; <br />bike.Brand = "Honda"; <br /><span style="color: #008000;"> // Create and instance of XmlSerializer class. </span> <br /> XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike)); <br /> <span style="color: #008000;"> // Create an instance of stream writer. </span> <br /> TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml"); <br /> <span style="color: #008000;">// Serialize the instance of xmlserusingXElement. </span> <br /> xmlSerializer.Serialize(txtWriter, bike); <br /> <span style="color: #008000;">// Close the stream writer </span> <br /> txtWriter.Close(); <br />} <br />} <br /> } <em><br /></em></pre><em> </em></code><em><p><code></code> </p></em></em></div><em><em> <div><em><strong>4<span style="color: #993366;">)XML Serialization using array of Objects:</span><br /> </strong>Serialize the array of objects into XML.<br /> <code> </code></em><code><pre><em>using System; <br />using System.Collections.Generic;<br />&nbsp;using System.Linq; <br />using System.Text;<br />&nbsp;using System.Xml.Serialization;<br />&nbsp;using System.IO;<br />&nbsp;namespace XmlSerUsingOjcOfArray <br />{ <br />class Bike <br />{ <br /> public string Name { get; set; } <br /> public string Price { get; set; } <br /> private void Main(string[] args) <br /> { <br /><span style="color: #008000;">//Create first instance of Bike class.</span> <br /> Bike bike1 = new Bike(); <br />bike1.Name = "Honda-City"; <br /> bike1.Price = "52190"; <br /><span style="color: #008000;">//Create second instance of Bike class. </span> <br />Bike bike2 = new Bike(); <br />bike2.Name = "Yamaha"; <br /> bike2.Price = "50000"; <br /><span style="color: #008000;">// Create an array of Bike </span> <br /> Bike[] bikes = new Bike[2]; <br />bikes[0] = bike1; <br />bikes[1] = bike2; <br /><span style="color: #008000;">//</span><span style="color: #008000;">Create and instance of XmlSerializer class. </span><br /> XmlSerializer xmlSerializer = new XmlSerializer(typeof(Bike[])); <br /> <span style="color: #008000;">// Create an instance of stream writer. </span><br />TextWriter txtWriter = new StreamWriter(@"C:\Users\pooja\Desktop\serialization.xml"); <br /> <span style="color: #008000;">// Serialize the instance of XmlSerUsingOjcOfArray. </span><br /> xmlSerializer.Serialize(txtWriter, bikes); <br /><span style="color: #008000;">// Close the stream writer </span><br /> txtWriter.Close(); <br /> } <br /> } <br />} </em> </pre> </code></div></em></em>

Answers (1)