XML serialization
object serialization
XML namespaces
programming
software development

How to serialize an object to XML without getting xmlns...?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When you serialize a C# object with XmlSerializer, the output usually includes xmlns:xsi and xmlns:xsd namespace declarations by default. If the consuming system expects namespace-free XML, the usual fix is to pass an empty XmlSerializerNamespaces object during serialization.

Why xmlns Shows Up by Default

XmlSerializer includes standard namespace declarations because it is generating valid XML that can represent schema-related concepts such as type information and nullable values.

A simple example:

csharp
1using System;
2using System.IO;
3using System.Xml.Serialization;
4
5public class Person
6{
7    public string Name { get; set; } = string.Empty;
8    public int Age { get; set; }
9}
10
11var serializer = new XmlSerializer(typeof(Person));
12var person = new Person { Name = "Alice", Age = 30 };
13
14using var writer = new StringWriter();
15serializer.Serialize(writer, person);
16Console.WriteLine(writer.ToString());

That output usually includes the namespace attributes even though the object model itself did not mention them explicitly.

Suppress the Default Namespace Declarations

To remove those default xmlns attributes, create an empty namespace mapping.

csharp
1using System;
2using System.IO;
3using System.Xml.Serialization;
4
5var serializer = new XmlSerializer(typeof(Person));
6var person = new Person { Name = "Alice", Age = 30 };
7
8var namespaces = new XmlSerializerNamespaces();
9namespaces.Add("", "");
10
11using var writer = new StringWriter();
12serializer.Serialize(writer, person, namespaces);
13Console.WriteLine(writer.ToString());

The important line is:

csharp
namespaces.Add("", "");

That tells the serializer to use an empty prefix and an empty namespace, which suppresses the usual default declarations.

Remove the XML Declaration Too If Needed

Sometimes the requirement is not only “no xmlns” but also “no XML declaration.” In that case, serialize through an XmlWriter with custom settings.

csharp
1using System.IO;
2using System.Xml;
3using System.Xml.Serialization;
4
5var settings = new XmlWriterSettings
6{
7    OmitXmlDeclaration = true,
8    Indent = true
9};
10
11var namespaces = new XmlSerializerNamespaces();
12namespaces.Add("", "");
13
14using var stringWriter = new StringWriter();
15using var xmlWriter = XmlWriter.Create(stringWriter, settings);
16
17serializer.Serialize(xmlWriter, person, namespaces);
18Console.WriteLine(stringWriter.ToString());

That gives you tighter control over the final XML shape.

Nested Objects Still Work

The empty-namespace approach also works with nested objects.

csharp
1public class Order
2{
3    public string Id { get; set; } = string.Empty;
4    public Person Customer { get; set; } = new Person();
5}

As long as the object graph itself does not require namespace-qualified types or elements, the serializer can emit clean nested XML without the default namespace declarations.

When You Should Not Remove Namespaces

Sometimes removing xmlns is the wrong move. If another system validates the XML against a schema or expects explicit namespaces, stripping them can make the XML invalid for that consumer.

So the question is not only “how do I remove them,” but also “should I remove them for this integration?”

If the partner system explicitly requires plain namespace-free XML, the empty namespace mapping is correct. If it expects schema-aware XML, keep the namespaces.

An Alternative: Build XML Manually

If you need very specific output, LINQ to XML gives full control.

csharp
1using System.Xml.Linq;
2
3var xml = new XElement("Person",
4    new XElement("Name", "Alice"),
5    new XElement("Age", 30)
6);
7
8Console.WriteLine(xml);

This avoids serializer defaults entirely, but it also means you are manually controlling the XML structure instead of reusing XmlSerializer.

Common Pitfalls

A common mistake is suppressing namespaces and then being surprised when another system rejects the XML because it expected schema-qualified elements.

Another pitfall is forgetting that null handling and schema-related features sometimes depend on namespace support.

Developers also sometimes think changing class attributes alone will remove the default serializer namespaces. In the common case, the empty XmlSerializerNamespaces object is still the practical fix.

Finally, if encoding matters, remember that StringWriter is character-based. If you need exact byte output, write through a stream with the desired encoding.

Summary

  • 'XmlSerializer adds default namespace declarations unless told otherwise.'
  • Pass XmlSerializerNamespaces with Add("", "") to suppress xmlns:xsi and xmlns:xsd.
  • Use XmlWriterSettings if you also want to omit the XML declaration.
  • Keep namespaces if the receiving system depends on schema-aware XML.
  • Use LINQ to XML when you need complete manual control over the final output.

Course illustration
Course illustration

All Rights Reserved.