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:
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.
The important line is:
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.
That gives you tighter control over the final XML shape.
Nested Objects Still Work
The empty-namespace approach also works with nested objects.
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.
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
- '
XmlSerializeradds default namespace declarations unless told otherwise.' - Pass
XmlSerializerNamespaceswithAdd("", "")to suppressxmlns:xsiandxmlns:xsd. - Use
XmlWriterSettingsif 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.

