XML Serialization in C#
Introduction
XML Serialization in C# allows you to convert an object into an XML format that can be easily stored, transferred, or reconstructed later. This process is essential for persisting data, communicating with web services, or simply exchanging data between different parts of a system.
Setting Up
To start with XML Serialization in C#, you need to include the System.Xml.Serialization
namespace in your project. This namespace contains the necessary classes to perform serialization and deserialization.
using System.Xml.Serialization;
Basic XML Serialization
Let's begin with a simple example where we serialize and deserialize a basic object. Consider a class Person
with properties Name
and Age
.
public class Person { public string Name { get; set; } public int Age { get; set; } }
Serializing an Object
To serialize an object, you need to create an instance of the XmlSerializer
class, passing the type of the object you want to serialize to its constructor. Then, use the Serialize
method to convert the object to XML.
Person person = new Person { Name = "John Doe", Age = 30 }; XmlSerializer serializer = new XmlSerializer(typeof(Person)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, person); string xml = writer.ToString(); Console.WriteLine(xml); }
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Name>John Doe</Name> <Age>30</Age> </Person>
Deserializing an Object
Deserialization is the process of converting XML back into an object. Use the Deserialize
method of the XmlSerializer
class to achieve this.
string xml = "<Person><Name>John Doe</Name><Age>30</Age></Person>"; XmlSerializer serializer = new XmlSerializer(typeof(Person)); using (StringReader reader = new StringReader(xml)) { Person person = (Person)serializer.Deserialize(reader); Console.WriteLine($"Name: {person.Name}, Age: {person.Age}"); }
Advanced XML Serialization
XML Serialization in C# also supports more advanced scenarios, such as serializing collections, handling namespaces, and controlling XML attributes.
Serializing Collections
You can serialize collections like lists or arrays in a similar manner. Consider a class Team
that contains a list of Person
objects.
public class Team { public List<Person> Members { get; set; } }
Serializing a Team
object would include serializing all its members.
Team team = new Team { Members = new List<Person> { new Person { Name = "John Doe", Age = 30 }, new Person { Name = "Jane Doe", Age = 25 } } }; XmlSerializer serializer = new XmlSerializer(typeof(Team)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, team); string xml = writer.ToString(); Console.WriteLine(xml); }
<Team xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Members> <Person> <Name>John Doe</Name> <Age>30</Age> </Person> <Person> <Name>Jane Doe</Name> <Age>25</Age> </Person> </Members> </Team>
Controlling XML Format with Attributes
You can control how your class and its members are serialized by using various attributes from the System.Xml.Serialization
namespace.
Example with Attributes
Consider customizing the XML output using attributes like XmlElement
, XmlAttribute
, and XmlIgnore
.
public class Person { [XmlElement("FullName")] public string Name { get; set; } [XmlAttribute("Age")] public int Age { get; set; } [XmlIgnore] public string Password { get; set; } }
Serialized output will now reflect these customizations.
Person person = new Person { Name = "John Doe", Age = 30, Password = "secret" }; XmlSerializer serializer = new XmlSerializer(typeof(Person)); using (StringWriter writer = new StringWriter()) { serializer.Serialize(writer, person); string xml = writer.ToString(); Console.WriteLine(xml); }
<Person Age="30" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <FullName>John Doe</FullName> </Person>