What is Xml Serialization

Lets first understand Serialization.
Serialization is the process of converting an object into a format that can be transferred over the network.

So, What is XML Serialization?

XML Serialization is the process of converting an object into XML file which is easy to read and portable over the network using a protocol such as HTTP.

How we can do XML Serialization? Let's understand this by writing C# code.

Create a Console Application Project in Visual Studio.

Add a class with any name, in this example I have named this class as XmlSerialize

See below code snippet.

1.  using  System;  
2.  using  System.Collections.Generic;  
3.  using  System.IO;  
4.  using  System.Linq;  
5.  using  System.Text;  
6.  using  System.Threading.Tasks;  
7.  using  System.Xml.Serialization;  
8.  namespace  ConsoleApplicationDemo {     
9.      public  class  XmlSerialize    {               
10.         public  int  ProductID {   
11.             get;   
12.             set;  
13.         }          
14.         public  string  ProductName {   
15.             get;   
16.             set;  
17.         }          
18.         public  int  Stock {   
19.             get;   
20.             set;  
21.         }          
22.         public  void  Save(string  filename)        {              
23.             using (var  fileStream =  new  FileStream(filename, FileMode.Create))            {                  
24.                 var  xmlFile =  new  XmlSerializer(typeof(XmlSerialize));                 
25.                 xmlFile.Serialize(fileStream,  this);             
26.             }         
27.         }     
28.     }  
29. }  


Write Below code in Program.cs

1.  static  void  Main(string[] args)        {              
2.      XmlSerialize  objXMLSerialize =  new  XmlSerialize();             
3.      objXMLSerialize.ProductID = 10001;             
4.      objXMLSerialize.ProductName =  "Laptop";             
5.      objXMLSerialize.Stock = 50;             
6.      objXMLSerialize.Save("Product.xml");                    
7.  }  

Run this code and you may see Product.xml file in bin -> Debug(or Release) folder.

1.  <? xml version = "1.0" ?> < XmlSerialize xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"  
2.  xmlns: xsd = "http://www.w3.org/2001/XMLSchema" >   < ProductID > 10001 < /ProductID>  < ProductName > Laptop < /ProductName>  < Stock > 50 < /Stock> < /XmlSerialize>   

If you want an attribute in XML

For eg, you want to display ProductID as an attribute, then add below attribute in .CS file

 [XmlAttribute]

So code will look like:

 [XmlAttribute]
 public int ProductID { get; set; }

After successful execution, newly created xml file will contain below lines

1.  <? xml version = "1.0" ?> < XmlSerialize xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"  
2.  xmlns: xsd = "http://www.w3.org/2001/XMLSchema"   
3.  ProductID = "10001" >   < ProductName > Laptop < /ProductName>  < Stock > 50 < /Stock> < /XmlSerialize>  

You may notice that ProductID is displaying as an attribute in above XML file.

Hope you understand the meaning of XML Serialization and how to implement XML Serialization in C#.

Happy Coding :)
Protected by Copyscape

Comments

Post a Comment

Dear Readers, Please post your valuable feedback in the comment section if you like this blog or if you have any suggestions. I would love to hear the same from you. Thanks

Popular

C# 6.0 New Features

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners