In this tutorial, we will learn how to use the C# tools and features to serialize and deserialize the XML files.
What Is Serialization?
Serialization refers to the process of converting an object into an easily-transmittable pattern over the network or stored as a file. Deserialization is the opposite of that.
Sample XML File
Before we begin, we start by setting up a basic XML file for demonstration purposes. For example, the following shows a basic XML file that represents the database configuration.
NOTE: The following file does not represent any real-world configuration or layout. This is for testing purposes and might be incorrect:
<DatabaseConfiguration>
<ServerName>localhost</ServerName>
<DatabaseName>public</DatabaseName>
<UserID>root</UserID>
<Password>password</Password>
</DatabaseConfiguration>
<DatabaseConfiguration>
<ServerName>remote</ServerName>
<DatabaseName>prod</DatabaseName>
<UserID>root</UserID>
<Password>complexpass</Password>
</DatabaseConfiguration>
</DatabaseConfigurations>
Once we have the sample file ready to go, we can proceed and discuss how to serialize and deserialize the file.
Setting Up the Class
The first thing that we need to do is create a class that corresponds to the layout of the elements that are provided in the XML file. This allows us to map the data that is stored in the XML file into a format or class that C# can understand.
In our case, we can define the class as follows:
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("DatabaseConfigurations")]
public class DatabaseConfigurations
{
[XmlElement("DatabaseConfiguration")]
public List<DatabaseConfiguration> Configurations { get; set; }
}
public class DatabaseConfiguration
{
[XmlElement("ServerName")]
public string ServerName { get; set; }
[XmlElement("DatabaseName")]
public string DatabaseName { get; set; }
[XmlElement("UserID")]
public string UserID { get; set; }
[XmlElement("Password")]
public string Password { get; set; }
}
In this case, we start by defining a class called “DatabaseConfiguration” that stores the data as list. The class also extracts each element from the XML file using a “get” and “set” accessor methods.
Serialize the Object
Once the object is ready, we can proceed and convert it into the XML format as shown in the following example code:
{
public static void Main()
{
DatabaseConfigurations dbConfigs = new DatabaseConfigurations
{
Configurations = new List<DatabaseConfiguration>
{
new DatabaseConfiguration
{
ServerName = "localhost",
DatabaseName = "public",
UserID = "root",
Password = "password"
},
new DatabaseConfiguration
{
ServerName = "remote",
DatabaseName = "prod",
UserID = "root",
Password = "complexpass"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(DatabaseConfigurations));
using (TextWriter writer = new StreamWriter("dbconfigs.xml"))
{
serializer.Serialize(writer, dbConfigs);
}
Console.WriteLine("XML file saved!");
}
}
What this code does is take the object that we created earlier which represents the layout of an XML file and convert it into an XML entity.
Deserialize the Object
We can read an XML file into a C# object which is also known as deserialization. An example is as follows:
using System.IO;
using System.Xml.Serialization;
public class Program
{
public static void Main()
{
XmlSerializer serializer = new XmlSerializer(typeof(DatabaseConfigurations));
DatabaseConfigurations dbConfigs;
using (TextReader reader = new StreamReader("config.xml"))
{
dbConfigs = (DatabaseConfigurations)serializer.Deserialize(reader);
}
foreach (var config in dbConfigs.Configurations)
{
Console.WriteLine($"ServerName: {config.ServerName}, DatabaseName: {config.DatabaseName}, UserID: {config.UserID}, Password: {config.Password}");
}
}
}
This should read the data from the XML file and convert it into a valid object.
Conclusion
This introduces you to the process of converting an object into an XML file and vice versa using the XML package in C#.