Skip to main content
An XML namespace is a URI that qualifies an element or attribute name, so that <Note> in a shipping vocabulary and <Note> in a billing vocabulary are different names. This article shows how to put a whole document in a namespace, how to give one element or attribute a namespace of its own, and what XmlSerializer checks when it reads a qualified document. JSON has no namespaces, so this part of the API has no System.Text.Json counterpart. The attribute names follow System.Xml.Serialization instead: [XmlRoot], [XmlElement], and [XmlAttribute] each have a Namespace property.

Put a document in a default namespace

Set XmlSerializerOptions.DefaultNamespace to put the document element of every type in one namespace. You don’t need to change the type.
Program.cs
Shipment.cs
The code produces the following output:
The serializer declares the namespace once, as the default namespace of the document element. The child elements have no prefix, so they are in the same namespace. View the complete sample on GitHub

Set the namespace of a type, an element, or an attribute

When the namespace belongs to the type and not to the application, declare it on the type. Three attributes take a namespace URI:
Invoice.cs
Program.cs
The code produces the following output:
Notice the following in the output:
  • Customer has no namespace of its own, so it is written without a prefix and inherits the namespace of Invoice.
  • Note is in a different namespace. The serializer declares that namespace as the default namespace of the Note element.
  • An attribute with no prefix is never in a namespace, so number stays unqualified. The revision attribute needs a prefix, and the writer generates one: ns1.
Without further instruction the serializer chooses how each namespace is declared, as it did here with ns1. The next section shows how to choose the prefixes yourself. View the complete sample on GitHub

Put an element in no namespace

An element with no namespace of its own takes the namespace of its parent. Some formats put the document element in a namespace and the elements inside it in none; an XML Schema does this unless it says elementFormDefault="qualified". Give such a member an empty namespace:
LegacyOrder.cs
Program.cs
The code produces the following output:
xmlns="" takes the element out of the default namespace. When the document is read, only an Id element in no namespace is this member: an Id in the namespace of the order is another element. [XmlArray(Namespace = "")] does the same for a wrapper element. View the complete sample on GitHub

Choose the prefixes

Prefixes carry no meaning in XML: a conforming reader treats ns1:revision and audit:revision as the same name. People and less careful consumers do read them, so you can state the prefixes a document is written with. A namespace that you declare is declared once, on the document element, and every element and attribute in it uses the prefix you gave. There are three ways to state them, and one place to find them.

Set them on the options

XmlSerializerOptions.Namespaces takes the declarations as a dictionary of prefix and namespace URI. An empty prefix is the default namespace.
Program.cs
The code produces the following output:
The declarations are written in the order of the dictionary, whether or not the document uses them. The one that names the document element’s own namespace comes first. The dictionary is copied when you set the property, and a declaration that XML doesn’t allow, such as one for the xmlns prefix, is an ArgumentException then. View the complete sample on GitHub

Declare them on the type

When the prefixes belong to the format and not to one call, put [XmlNamespaceDeclaration] on the type that is the document element. The attribute can be repeated.
PodcastShow.cs
Program.cs
The code produces the following output:
The declarations of the options are written first and take precedence: a declaration on the type is left out when the options already declare its prefix or its namespace. The attribute has no effect where the type is a member of another type, because only a document element carries the declarations. View the complete sample on GitHub

Use the prefixes of a well-known format

KnownNamespaces has the customary prefixes of some common formats, ready to assign.
Program.cs
The code produces the following output. The start tag is one line; it is wrapped here to fit.
Every declaration of a set is written. A set holds the namespaces a format adds, not the format’s own: RSS elements have no namespace, and a sitemap’s are customarily in the default namespace. View the complete sample on GitHub

Take them from an existing document

XmlDocument.DiscoverNamespaces reads a document and returns the prefixes it declares, in order, as a dictionary you can assign to Namespaces. Use it when what you write has to look like a document you were given.
Program.cs
The code produces the following output:
DiscoverNamespaces looks at every start tag, so it finds s, which the document declares on an inner element. A prefix that the document binds more than once is listed with its first namespace. A default namespace is listed only when the document element declares it: one that an inner element declares names that element’s content alone. There are overloads for a string, UTF-8 bytes, and a stream. View the complete sample on GitHub

What a prefix on the document element changes

Two rules decide how the document element itself is written when one of the declarations names its namespace:
  • Declared as the default namespace (an empty prefix), the element has no prefix, as it has without any declarations.
  • Declared with a prefix only, the element is written with that prefix, such as <bill:Invoice>.
In the second case there is no default namespace, so a member with no namespace of its own, such as Customer, is written as <Customer> in no namespace. It no longer inherits the namespace of the document element. XmlSerializer still reads the document, because it matches such a member by local name, but another consumer sees a different name. Either declare the namespace as the default namespace too, or give each member its namespace with [XmlElement].
When the document element has a namespace, and the declarations give that namespace no prefix but declare a different default namespace, both can’t be written on one element. Serialization throws an InvalidOperationException.

Read a document that uses prefixes

When the serializer reads, it compares local names and namespace URIs. It never compares prefixes. The following document binds every namespace to a prefix of its own choosing, and it deserializes into the same Invoice type.
Program.cs
The code produces the following output:
View the complete sample on GitHub

Know what the serializer checks

How strictly a name is matched depends on whether the type gives it a namespace. Two element members can map to the same local name when their namespaces differ, as link and atom:link do in an RSS feed. Each element goes to the member that names its namespace, and the member with no namespace takes the rest. Two element members with the same local name and the same namespace are an InvalidOperationException when the type is first used. Attribute members follow the same rule: two can share a local name when their namespaces differ, as href and xlink:href do on a link, and two with the same local name and the same namespace are an InvalidOperationException. An attribute with no prefix is in no namespace, so an attribute member with no namespace takes only the unprefixed attribute. An attribute of that local name in a namespace no member names is unmapped. A document element in the wrong namespace is an error:
Program.cs
The code produces the following output:
A child element or attribute whose namespace doesn’t match is not an error by default. The serializer treats it as content the type doesn’t map, and skips it. To reject it or to keep it, see Handle content that no member maps. View the complete sample on GitHub

Namespaces the serializer writes for you

Some features add an attribute in a namespace of their own. The serializer declares the namespace on the element that carries the attribute, unless a declaration of it is already in scope: an element inside one that declared it reuses that declaration, and a namespace declared with XmlSerializerOptions.Namespaces is declared once, on the document element.

Compare qualified names in code

XmlName is the type the library uses for a qualified name: a local name, a namespace URI, and an optional prefix. Two names are equal when the local name and the URI are equal. The prefix only affects how the name is printed. XmlNamespaces holds the URIs of the well-known namespaces, and XmlAttributes holds the local names nil and type. Use them when you compare names with the reader.
Program.cs
The code produces the following output:
View the complete sample on GitHub

Work with namespaces below the serializer

The reader and the writer give you full control when the serializer’s choices aren’t enough:
  • Utf8XmlReader resolves every prefix as it reads. Compare names with LocalNameEquals and NamespaceUriEquals, and never by prefix.
  • Utf8XmlWriter has overloads of WriteStartElement and WriteAttributeString that take a prefix, a local name, and a namespace URI, and WriteDefaultNamespace for an xmlns declaration.

See also

Customize names

Rename elements and attributes, and choose which members are attributes.

Null and nil

How xsi:nil marks a null value.

Use Utf8XmlWriter

Write prefixes and namespace declarations yourself.

XmlName

API reference for qualified names.