In this article, we learn about XML-based Serialization and look at ways to achieve XML Serialization in C#. Serialization is an essential concept in software development that allows us to convert complex objects into a format suitable for storage or transmission. In C#, one of the commonly used serialization formats is XML (eXtensible Markup Language). XML provides a human-readable and platform-independent way to represent data. In this article, we will explore XML serialization in C# and learn how to serialize and deserialize objects using the .NET framework.
Table of Contents
The System.Xml.Serialization Namespace
C# provides a dedicated namespace, System.Xml.Serialization
which contains classes and attributes for XML serialization. This namespace offers a powerful set of tools to facilitate the process of converting objects to and from XML.
XML Serialization Attributes
To control the serialization process, we can use attributes provided by the System.Xml.Serialization
namespace. Here are some commonly used attributes:
- XmlRootAttribute: Specifies the XML root element name for the serialized object.
- XmlElementAttribute: Specifies the XML element name for a particular member of a class.
- XmlAttributeAttribute: Specifies that a member should be serialized as an XML attribute rather than an element.
- XmlIgnoreAttribute: Excludes a member from the serialization process.
- XmlArrayAttribute: Specifies that a member represents an array of elements in the XML structure.
Basic XML Serialization in C#
To serialize an object to XML, we need to perform the following steps:
- Define the classes and their members that we want to serialize.
- Apply serialization attributes to control the serialization behaviour.
- Use a serializer to convert the object into XML.
Let’s consider a simple example where we have a Person
class representing a person’s information:
public class Person{ public string Name { get; set; } public int Age { get; set; }}
To serialize an instance of the Person
class, we can use the XmlSerializer
class from the System.Xml.Serialization
namespace:
Person person = new Person { Name = "John Doe", Age = 30 };XmlSerializer serializer = new XmlSerializer(typeof(Person));using (TextWriter writer = new StreamWriter("person.xml")){ serializer.Serialize(writer, person);}
In the above code, we create an instance of the Person
class and initialize its properties. Then, we create an instance of the XmlSerializer
class, passing the type of object we want to serialize as a parameter. We use a TextWriter
(in this case, a StreamWriter
) to specify where we want to store the XML data. Finally, we call the Serialize
method of the serializer, passing the writer and the object to be serialized.
After running the code, a file named “person.xml” will be created with the following content:
<?xml version="1.0" encoding="utf-8"?><Person> <Name>John Doe</Name> <Age>30</Age></Person>
XML Deserialization
Deserialization is the process of converting XML data back into an object. We can achieve this using the XmlSerializer
class as well. Here’s an example:
XmlSerializer serializer = new XmlSerializer(typeof(Person));using (TextReader reader = new StreamReader("person.xml")){ Person deserializedPerson = (Person)serializer.Deserialize(reader);}
In this code snippet, we create an instance of the XmlSerializer
class, passing the type of object we want to deserialize. We use a TextReader (in this case, a StreamReader) to read the XML data from a file. Then, we call the Deserialize method of the serializer, passing the reader as a parameter. A result is an object of the type Person
that contains the deserialized data.
Handling XML Namespace
When dealing with XML serialization, we might encounter scenarios where the XML contains namespaces. To handle namespaces, we can use the XmlSerializerNamespaces
class:
XmlSerializer serializer = new XmlSerializer(typeof(Person));using (TextWriter writer = new StreamWriter("person.xml")){ XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add("custom", "http://example.com/custom-namespace"); serializer.Serialize(writer, person, namespaces);}
In the above code, we create an instance of the XmlSerializerNamespaces
class and add the desired namespaces using the Add
method. We then pass the namespaces object as the third parameter to the Serialize
method.
Conclusion
XML serialization plays a crucial role in modern software development, allowing us to persist and exchange object data in a platform-independent manner. In this article, we explored the basics of XML serialization in C# using the System.Xml.Serialization
namespace. We learned how to serialize and deserialize objects using the XmlSerializer
class and how to handle namespaces. With this knowledge, you can apply XML serialization techniques to manage data in your C# applications efficiently.
References – Examples of XML Serialization in C#
You can also check my other trending articles on .NET Core to learn more about developing .NET Core Applications
- Microsoft Feature Management – Feature Flags in ASP.NET Core C# – Detailed Guide
- Microservices with ASP.NET Core 3.1 – Ultimate Detailed Guide
- Entity Framework Core in ASP.NET Core 3.1 – Getting Started
- Series: ASP.NET Core Security – Ultimate Guide
- ML.NET – Machine Learning with .NET Core – Beginner’s Guide
- Real-time Web Applications with SignalR in ASP.NET Core 3.1
- Repository Pattern in ASP.NET Core with Adapter Pattern
- Creating an Async Web API with ASP.NET Core – Detailed Guide
- Build Resilient Microservices (Web API) using Polly in ASP.NET Core