How to Serialize and Deserialize an XML File into a C# Object (and Vice-Versa)

Introduction

 
In this article, we will see how to serialize and deserialize an XML file to a C# object, and convert C# object into an XML file.
 

Serializing XML to C# Object

 
Let's understand how to convert an XML file into a C# object. Take note of the below small XML file to demonstrate.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  3.   <Employee name="x" age="30" />  
  4.   <Employee name="y" age="32" />  
  5. </Company>  
To convert this XML into an object, first you need to create a similar class structure in C#. 
  1. [XmlRoot(ElementName = "Company")]  
  2. public class Company  
  3. {  
  4.     public Company()  
  5.     {  
  6.         Employees = new List<Employee>();  
  7.     }  
  8.   
  9.     [XmlElement(ElementName = "Employee")]  
  10.     public List<Employee> Employees { getset; }  
  11.   
  12.     public Employee this[string name]  
  13.     {  
  14.         get { return Employees.FirstOrDefault(s => string.Equals(s.Name, name, StringComparison.OrdinalIgnoreCase)); }  
  15.     }  
  16. }  
  17.   
  18. public class Employee  
  19. {  
  20.     [XmlAttribute("name")]  
  21.     public string Name { getset; }  
  22.   
  23.     [XmlAttribute("age")]  
  24.     public string Age { getset; }  
  25. }  
Your XML and C# objects are ready. Let's see the final step of converting XML into a C# object. To do that, you need to use System.Xml.Serialization.XmlSerializer to serialize it. 
  1. public T DeserializeToObject<T>(string filepath) where T : class  
  2. {  
  3.     System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));  
  4.   
  5.     using (StreamReader sr = new StreamReader(filepath))  
  6.     {  
  7.         return (T)ser.Deserialize(sr);  
  8.     }  
  9. }  
Use the XML file path and use this function. You should see that the XML is converted into a company object with two employee objects. 
 

Deserializing a C# Object in XML

 
Create a C# object, such as a company with a few employees, and then convert it into an XML file. 
  1. var company = new Company();  
  2.            company.Employees = new List<Employee>() { new Employee() { Name = "o", Age = "10" } };  
  3.            SerializeToXml(company, xmlFilePath);   
  1. public static void SerializeToXml<T>(T anyobject, string xmlFilePath)  
  2. {  
  3.     XmlSerializer xmlSerializer = new XmlSerializer(anyobject.GetType());  
  4.   
  5.     using (StreamWriter writer = new StreamWriter(xmlFilePath))  
  6.     {  
  7.         xmlSerializer.Serialize(writer, anyobject);  
  8.     }  
  9. }  
The output should look like the below text after converting it into XML.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Company xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  3.   <Employee age="10" name="o"/>  
  4. </Company>  

Conclusion

 
In this post, we learned how to serialize and deserialize an XML file to a C# object and vice versa.
Next Recommended Reading Serializing Object to XML in C#