XmlSerializer
namespaces
xsi
xsd
.NET

XmlSerializer remove unnecessary xsi and xsd namespaces

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

XmlSerializer often adds the xmlns:xsi and xmlns:xsd namespace declarations by default, even when the receiving system does not care about XML Schema metadata. If you want cleaner XML output, the usual fix is to serialize with an explicitly empty XmlSerializerNamespaces instance instead of relying on the serializer's defaults.

Why Those Namespaces Appear

The serializer commonly emits:

xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

These declarations are not random noise. They are part of XML schema conventions, and the serializer includes them because they may be needed for schema-related constructs such as xsi:nil or type metadata.

If your XML is being used only as a simple data payload, they are often unnecessary.

The Standard Way to Remove Them

Create an empty namespace collection and pass it into Serialize:

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

That usually produces XML without the xsi and xsd namespace declarations.

What the Output Looks Like

Instead of:

xml
1<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3  <Name>Mark</Name>
4  <Age>30</Age>
5</Person>

You get the cleaner form:

xml
1<Person>
2  <Name>Mark</Name>
3  <Age>30</Age>
4</Person>

That is often easier to compare in tests and friendlier for systems that expect a minimal XML payload.

Namespace Removal Is Separate from the XML Declaration

Developers often mix up namespace declarations with the XML declaration line such as <?xml version="1.0" encoding="utf-16"?>. These are different concerns.

If you also want to control the declaration, use an XmlWriter with settings:

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

This lets you control both namespaces and the declaration separately.

Cases Where You Should Keep the Namespaces

Do not remove the schema namespaces blindly. They may be required when:

  • the receiving system validates against an XSD
  • your XML uses xsi:nil
  • polymorphic serialization needs schema-instance metadata

If those features are in play, stripping the namespaces can break interoperability rather than improving the payload.

Attributes on the Model Can Reintroduce Namespaces

Even if you pass an empty namespace collection, attributes such as XmlRoot, XmlType, or XmlElement with explicit namespaces can still produce namespaced output. For example:

csharp
1[XmlRoot("Person", Namespace = "http://example.com/contracts")]
2public class Person
3{
4    public string Name { get; set; } = "";
5}

In that case the serializer is doing what your model requested. Removing xsi and xsd does not mean "remove all namespaces under all circumstances."

Common Pitfalls

The biggest mistake is creating XmlSerializerNamespaces but forgetting to pass it into Serialize. Until the serializer actually receives that namespace collection, the default schema namespaces still appear.

Another issue is assuming all namespaces are unnecessary. Some XML contracts genuinely depend on namespace-qualified elements or schema-instance features, so stripping them only because the output "looks cleaner" can create subtle compatibility bugs.

Finally, be careful when comparing XML in tests as raw strings. Whitespace, declarations, and namespace prefixes can vary. If structural equality matters, parsing the XML and comparing nodes is often more robust than literal string comparison.

Summary

  • Use XmlSerializerNamespaces with ns.Add("", "") to suppress default xsi and xsd declarations.
  • Pass that namespace collection directly into serializer.Serialize(...).
  • Namespace suppression is separate from controlling the XML declaration.
  • Keep the schema namespaces when the XML contract actually depends on them.
  • Explicit namespace attributes on your model can still produce namespaced output.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.