What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
To suppress the commonly generated xmlns:xsi and xmlns:xsd declarations, pass an XmlSerializerNamespaces containing an empty prefix and empty namespace URI to the serializer’s three-argument Serialize overload:
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "");
serializer.Serialize(writer, value, namespaces);
This is appropriate when the XML contract and serialized model do not require namespaces. It does not remove meaningful namespaces or make required attributes such as xsi:nil safe to discard.
Complete example
This example serializes a namespace-free model and supplies the empty namespace mapping:
using System;
using System.IO;
using System.Xml.Serialization;
[XmlRoot("person")]
public class Person
{
public string Name { get; set; }
}
public static class Demo
{
public static void Main()
{
var person = new Person { Name = "Ada" };
var serializer = new XmlSerializer(typeof(Person));
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "");
using var writer = new StringWriter();
serializer.Serialize(writer, person, namespaces);
Console.WriteLine(writer.ToString());
}
}
The output shape is:
<?xml version="1.0" encoding="utf-16"?>
<person>
<Name>Ada</Name>
</person>
Exact whitespace and lexical output can vary with writer settings and the model. The empty mapping is a supported way to provide namespace mappings during serialization; the original focused example uses the same Add("", "") approach (DZone tutorial).
#1 Best Overall
What the declarations mean
A common default output for a simple type may look like this:
<MyModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</MyModel>
These are namespace declarations: xmlns:xsi binds the prefix xsi to the XML Schema instance namespace, and xmlns:xsd binds xsd to the XML Schema namespace. They are not schema imports, and their presence alone does not mean that the serializer loaded or validated an XSD. XML defines xmlns declarations as the mechanism for binding prefixes to namespace names (W3C Namespaces in XML).
The empty mapping suppresses redundant declarations from the serializer’s default namespace set when nothing in the document needs them. It does not strip namespaces explicitly assigned to the root, a type, a member, or a nested object. The exact output depends on the model, attributes, null values, polymorphic values, and runtime configuration; do not assume every XmlSerializer setup emits identical text.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsRank #2
When a namespace must stay
If a contract assigns the root to a namespace, for example:
[XmlRoot("person", Namespace = "urn:example:people")]
public class Person
{
public string Name { get; set; }
}
the namespace is part of the element’s expanded name (the combination of its local name and namespace URI). The serialized document may therefore need a default declaration such as xmlns="urn:example:people". Removing it would change the element’s meaning and may break validation, deserialization, or a receiving system’s contract. Empty namespace mappings are not a way to turn a namespace-qualified model into a namespace-free one.
For an intentionally namespaced model, provide mappings that match the contract:
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("p", "urn:example:people");
namespaces.Add("a", "urn:example:address");
serializer.Serialize(writer, person, namespaces);
Prefixes such as p and a are labels for namespace URIs; XML consumers should identify names by URI rather than rely on a particular prefix spelling. In practice, some legacy consumers incorrectly compare prefixes or whole XML strings, so confirm any such requirement against the actual integration contract.
Check for xsi:nil and xsi:type
Namespace declarations are required whenever a prefixed name uses them. A nullable member configured to serialize a null value as nil can produce output like:
<Value xsi:nil="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
Here, xsi is not redundant: it gives the nil attribute its namespace. Similar care applies to xsi:type and other namespace-qualified names. If null values should be omitted instead of represented as nil, review the model mapping, for example [XmlElement(IsNullable = false)] where that behavior fits the contract. Omitting a nil element changes the serialized representation and can change its meaning; it is a modeling choice, not just formatting.
Rank #4
Before suppressing declarations, search the output for uses of prefixes such as xsi: or xsd:, not only for the declarations themselves. A prefix used in an element or attribute must be bound by a declaration in scope (W3C Namespaces in XML).
The XML declaration and encoding are separate
XmlSerializerNamespaces controls namespace mappings. It does not remove the XML declaration, such as <?xml version="1.0" encoding="utf-16"?>, nor does it set the encoding of output bytes.
To omit the declaration, configure an XmlWriter:
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
Encoding = Encoding.UTF8
};
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "");
using var stream = new MemoryStream();
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(xmlWriter, model, namespaces);
}
string xml = Encoding.UTF8.GetString(stream.ToArray());
The stream example writes UTF-8 bytes. By contrast, a StringWriter stores characters in a .NET string and reports UTF-16, so its XML declaration commonly says encoding="utf-16". That declaration is not evidence that namespace suppression failed. For files or network output, use a stream or writer whose actual byte encoding matches the receiving system’s requirement. See Microsoft’s documentation for OmitXmlDeclaration.
Best Value
Do not remove declarations with string replacement
A replacement such as xml.Replace(" xmlns:xsi=...", "") treats XML as plain text. It depends on exact whitespace, attribute order, and prefix spelling; it can also remove a declaration still needed by xsi:nil or xsi:type, leaving an unbound prefix and invalid XML. Passing namespace mappings to serialization is safer because it lets the serializer produce the document with those mappings in view.
Troubleshooting and verification
- The declarations remain: Check
[XmlRoot],[XmlType],[XmlElement], and related attributes for namespaces. Inspect nested objects, polymorphic values, null members, and custom serialization code. - A declaration disappeared but serialization now fails downstream: Look for prefixed names such as
xsi:nilthat still require a binding, or for a namespace-qualified element that the receiver expects. - The consumer says it requires “no namespaces,” but the model has a target namespace: That is a contract conflict. If possible, use a dedicated namespace-free DTO for that wire format rather than altering the canonical model’s mapping.
- The XML still contains
encoding="utf-16": That is separate from namespace handling and commonly results from usingStringWriter. - Only formatting differs: Use
XmlWriterSettingsfor indentation and declaration behavior. Those settings do not replace namespace mappings.
When testing, compare parsed XML structure and namespace URIs rather than exact attribute order or prefix spelling, unless the receiving system genuinely imposes a textual requirement. If you must match a namespace-free legacy format while preserving a differently namespaced domain model, use an explicit transport DTO; reserve manual XML transformations or IXmlSerializable for cases the normal mapping cannot express.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

