<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
SetXmlSerializerOptions.DefaultNamespace to put the document element of every type in one namespace. You don’t need to change the type.
Program.cs
Shipment.cs
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
Customerhas no namespace of its own, so it is written without a prefix and inherits the namespace ofInvoice.Noteis in a different namespace. The serializer declares that namespace as the default namespace of theNoteelement.- An attribute with no prefix is never in a namespace, so
numberstays unqualified. Therevisionattribute needs a prefix, and the writer generates one:ns1.
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 sayselementFormDefault="qualified". Give such a member an empty namespace:
LegacyOrder.cs
Program.cs
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 treatsns1: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
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
Use the prefixes of a well-known format
KnownNamespaces has the customary prefixes of some common formats, ready to assign.
Program.cs
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
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>.
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 sameInvoice type.
Program.cs
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
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 withXmlSerializerOptions.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
Work with namespaces below the serializer
The reader and the writer give you full control when the serializer’s choices aren’t enough:Utf8XmlReaderresolves every prefix as it reads. Compare names withLocalNameEqualsandNamespaceUriEquals, and never by prefix.Utf8XmlWriterhas overloads ofWriteStartElementandWriteAttributeStringthat take a prefix, a local name, and a namespace URI, andWriteDefaultNamespacefor anxmlnsdeclaration.
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.