XML Serialization
XMLSerializer
Plain XML
C# Programming
Data Serialization

How can I make the xmlserializer only serialize plain xml?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

When people ask for "plain XML" from XmlSerializer, they usually mean XML without the default namespace declarations and without extra wrapper noise they did not ask for. The serializer can be controlled, but only within the XML shape implied by your model and attributes.

Remove the Default Namespace Declarations

The most common complaint is output like this:

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

To suppress those declarations, provide an empty namespace mapping during serialization.

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

That is the normal answer when the only goal is to remove namespace clutter.

Control Element Names with Attributes

If the output still does not look "plain," the next step is usually to define explicit XML names rather than relying on class and property names.

csharp
1using System.Xml.Serialization;
2
3[XmlRoot("person")]
4public class Person
5{
6    [XmlElement("name")]
7    public string Name { get; set; } = "";
8}

This gives you cleaner and more predictable XML element names.

Without attributes, XmlSerializer uses default naming conventions based on the .NET type model.

Remove the XML Declaration If Needed

Sometimes "plain XML" also means "no XML declaration line." That is writer behavior, not serializer behavior.

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};
11
12var serializer = new XmlSerializer(typeof(Person));
13var ns = new XmlSerializerNamespaces();
14ns.Add("", "");
15
16var sb = new StringBuilder();
17using var stringWriter = new StringWriter(sb);
18using var xmlWriter = XmlWriter.Create(stringWriter, settings);
19
20serializer.Serialize(xmlWriter, new Person { Name = "Ava" }, ns);
21
22Console.WriteLine(sb.ToString());

Now the output is cleaner if the receiving system expects only the element tree.

Exclude Unwanted Properties

If the serializer is including members you do not want, mark them with attributes such as XmlIgnore.

csharp
1public class Person
2{
3    public string Name { get; set; } = "";
4
5    [XmlIgnore]
6    public string InternalNote { get; set; } = "";
7}

This is the right fix when the XML is structurally correct but still contains application-only data.

You can also use:

  • 'XmlAttribute'
  • 'XmlElement'
  • 'XmlArray'
  • 'XmlArrayItem'
  • 'XmlText'

Those let you shape the output deliberately instead of hoping the default serializer layout matches the required XML contract.

Know the Limits

XmlSerializer does not mean "serialize arbitrary existing XML verbatim." It serializes objects into XML according to a mapping model.

So if you want:

  • exact whitespace preservation
  • mixed-content control
  • attribute ordering control
  • arbitrary raw XML fragments everywhere

then XmlSerializer may not be the right tool.

For strict XML-contract generation, it is good. For handcrafted document production, XmlWriter or LINQ to XML may be a better fit.

Embed Raw XML Only When Needed

If part of the requirement is "include this fragment as XML, not escaped text," XmlSerializer will not magically interpret ordinary strings as raw XML.

In that case, consider a strongly typed submodel or a different XML construction strategy. Treating raw XML as a string usually produces escaped output, which is correct serializer behavior.

That is often the real reason the result does not feel "plain."

Common Pitfalls

  • Expecting XmlSerializer to omit namespaces without supplying empty XmlSerializerNamespaces.
  • Confusing serializer output control with writer output control such as XML declaration omission.
  • Assuming plain strings will be inserted as raw XML fragments.
  • Relying on default member naming when the XML contract expects specific element names.
  • Using XmlSerializer for XML-shaping needs that really call for XmlWriter or LINQ to XML.

Summary

  • To get plainer XML, the first step is usually removing default namespace declarations.
  • Use XmlSerializerNamespaces with empty values to suppress those namespaces.
  • Control element names and excluded properties with serialization attributes.
  • Use XmlWriterSettings if you also want to omit the XML declaration.
  • If you need full handcrafted XML control, XmlSerializer may not be the right tool.

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.