Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use XmlSerializer in C#?
The XmlSerializer in C# is used to convert objects to XML format (serialization) and back to objects (deserialization). This allows you to store object data in XML files or transmit data between applications in a standardized XML format.
XmlSerializer provides fine-grained control over how objects are encoded into XML, including element names, attributes, and namespaces through various attributes.
Syntax
Following is the syntax for creating an XmlSerializer instance −
XmlSerializer serializer = new XmlSerializer(typeof(ClassName));
Following is the syntax for serializing an object to XML −
using (StreamWriter writer = new StreamWriter(filePath)) {
serializer.Serialize(writer, objectInstance);
}
Following is the syntax for deserializing XML back to an object −
using (StreamReader reader = new StreamReader(filePath)) {
ClassName obj = (ClassName)serializer.Deserialize(reader);
}
Required Classes
To perform XML serialization and deserialization, you need the following classes −
- XmlSerializer − Handles the conversion between objects and XML
- StreamWriter − Writes the serialized XML to a file or stream
- StreamReader − Reads XML data from a file or stream for deserialization
Using XmlSerializer for Serialization
Example
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public Person() { }
public Person(string name, int age, string email) {
Name = name;
Age = age;
Email = email;
}
}
public class Program {
public static void Main() {
Person person = new Person("John Doe", 30, "john@example.com");
string filePath = "person.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (StreamWriter writer = new StreamWriter(filePath)) {
serializer.Serialize(writer, person);
}
Console.WriteLine("Object serialized to " + filePath);
string xmlContent = File.ReadAllText(filePath);
Console.WriteLine("XML Content:");
Console.WriteLine(xmlContent);
}
}
The output of the above code is −
Object serialized to person.xml XML Content: <?xml version="1.0" encoding="utf-8"?> <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> <Email>john@example.com</Email> </Person>
Using XmlSerializer for Deserialization
Example
using System;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Settings {
public string Theme { get; set; }
public bool NotificationsEnabled { get; set; }
public int MaxConnections { get; set; }
public Settings() { }
}
public class Program {
public static void Main() {
Settings originalSettings = new Settings {
Theme = "Dark",
NotificationsEnabled = true,
MaxConnections = 100
};
string filePath = "settings.xml";
// Serialize to XML
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
using (StreamWriter writer = new StreamWriter(filePath)) {
serializer.Serialize(writer, originalSettings);
}
Console.WriteLine("Settings serialized to XML");
// Deserialize from XML
Settings loadedSettings;
using (StreamReader reader = new StreamReader(filePath)) {
loadedSettings = (Settings)serializer.Deserialize(reader);
}
Console.WriteLine("Settings loaded from XML:");
Console.WriteLine("Theme: " + loadedSettings.Theme);
Console.WriteLine("Notifications: " + loadedSettings.NotificationsEnabled);
Console.WriteLine("Max Connections: " + loadedSettings.MaxConnections);
}
}
The output of the above code is −
Settings serialized to XML Settings loaded from XML: Theme: Dark Notifications: True Max Connections: 100
Key Requirements
The class must be public and have a parameterless constructor.
Only public properties and fields are serialized by default.
Complex types used as properties must also be serializable.
Use
[XmlIgnore]attribute to exclude specific members from serialization.
Conclusion
XmlSerializer in C# provides a powerful way to convert objects to XML format and back. It requires classes to have parameterless constructors and only serializes public members by default, making it ideal for configuration files, data exchange, and persistent storage scenarios.
