How to Use XmlSerializer to serialize in C#
By FoxLearn 3/1/2025 3:30:25 AM 64
Steps to Serialize Objects in C# Using XmlSerializer
Define the class to serialize: First, create the class that you want to serialize. The class must have public properties or fields that you want to include in the XML. Optionally, you can use attributes like
[XmlElement]
and[XmlIgnore]
to control how the XML is generated.Create an instance of
XmlSerializer
: You will create an instance of theXmlSerializer
class, passing the type of the object to be serialized.Serialize the object: Use the
Serialize
method of theXmlSerializer
instance to convert the object into XML.Write the XML to a file or string: You can either serialize the object to a string using a
StringWriter
, or you can write it directly to a file using aStreamWriter
.
How to serialize an object using XmlSerializer
in C#?
For example, Using XmlSerializer
to Serialize a Book Object.
static string GetXml(object obj) { XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); using (var writer = new StringWriter()) { xmlSerializer.Serialize(writer, obj); return writer.ToString(); } }
You’ll need to ensure the class you want to serialize has the [Serializable]
attribute:
[Serializable] public class Book { public string Title { get; set; } public string Author { get; set; } public int YearPublished { get; set; } public List<string> Genres { get; set; } }
Creating a Book Object and Serializing It
static void Main(string[] args) { var book = new Book() { Title = "The Lean Startup", Author = "Eric Ries", YearPublished = 2011, Genres = new List<string> { "Business", "Entrepreneurship", "Technology" } }; string xml = GetXml(book); Console.WriteLine(xml); }
Output XML:
<?xml version="1.0" encoding="utf-16"?> <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Title>The Lean Startup</Title> <Author>Eric Ries</Author> <YearPublished>2011</YearPublished> <Genres> <string>Business</string> <string>Entrepreneurship</string> <string>Technology</string> </Genres> </Book>
Customizing the Serialization:
If you'd like to modify the output in different ways, here are some possible changes:
1. Removing the Namespace Attribute: By default, the XML includes the xmlns:xsi
namespace.
To remove it, you can pass an XmlSerializerNamespaces
object with XmlQualifiedName.Empty
:
static string GetXml(object obj) { XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); using (var writer = new StringWriter()) { xmlSerializer.Serialize(writer, obj, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty })); return writer.ToString(); } }
Output:
<?xml version="1.0" encoding="utf-16"?> <Book> <Title>The Lean Startup</Title> <Author>Eric Ries</Author> <YearPublished>2011</YearPublished> <Genres> <string>Business</string> <string>Entrepreneurship</string> <string>Technology</string> </Genres> </Book>
2. Change the Encoding to UTF-8: To change the encoding from UTF-16 to UTF-8, you can subclass StringWriter
:
public class Utf8StringWriter : StringWriter { public override Encoding Encoding => Encoding.UTF8; }
Then use this in your serialization method:
static string GetXml(object obj) { XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); using (var writer = new Utf8StringWriter()) { xmlSerializer.Serialize(writer, obj, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty })); return writer.ToString(); } }
Output:
<?xml version="1.0" encoding="utf-8"?> <Book> <Title>The Lean Startup</Title> <Author>Eric Ries</Author> <YearPublished>2011</YearPublished> <Genres> <string>Business</string> <string>Entrepreneurship</string> <string>Technology</string> </Genres> </Book>
3. Exclude a Property from Serialization: If you don't want to serialize the Genres
property, you can use the [XmlIgnore]
attribute:
[Serializable] public class Book { public string Title { get; set; } public string Author { get; set; } public int YearPublished { get; set; } [XmlIgnore] public List<string> Genres { get; set; } }
Output:
<?xml version="1.0" encoding="utf-8"?> <Book> <Title>The Lean Startup</Title> <Author>Eric Ries</Author> <YearPublished>2011</YearPublished> </Book>
4. Change Property Name in XML: You can change the serialized property name using the [XmlElement]
attribute.
For example, you can change YearPublished
to PublishedYear
:
[Serializable] public class Book { public string Title { get; set; } public string Author { get; set; } [XmlElement("PublishedYear")] public int YearPublished { get; set; } public List<string> Genres { get; set; } }
Output:
<?xml version="1.0" encoding="utf-8"?> <Book> <Title>The Lean Startup</Title> <Author>Eric Ries</Author> <PublishedYear>2011</PublishedYear> <Genres> <string>Business</string> <string>Entrepreneurship</string> <string>Technology</string> </Genres> </Book>
Note: When using XmlWriter, you don't need to apply the [Serializable]
attribute to your class. This allows you to serialize any class, including third-party classes that don't have this attribute. However, keep in mind that XmlWriter doesn't consider attributes like [XmlIgnore]
, so all properties will be serialized unless manually excluded.
In this example, I showed how you can use XmlSerializer
with a Book
class, and how you can customize serialization by modifying encoding, changing property names, excluding properties, and removing namespace attributes from the XML output.