What is Xml Serialization

In this blog, we will see XML Serialization and its implementation using C# code.
Lets first understand Serialization.
Serialization is the process of converting an object into a format that can be transferred over the network.

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.publicclass  XmlSerialize    {               
10. publicint  ProductID {   
11.             get;   
12.             set;  
13.         }          
14. public  string  ProductName {   
15.             get;   
16.             set;  
17.         }          
18. publicint  Stock {   
19.             get;   
20.             set;  
21.         }          
22. publicvoid  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.staticvoid  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#.
Please follow and like us:

2 thoughts on “What is Xml Serialization”

Leave a Comment