Skip to main content
This article is for developers who know System.Text.Json and are picking up XML. XmlEssentials is shaped like System.Text.Json on purpose, so most of what you know carries over under a different prefix. The first half of this article is the map from one API to the other. The second half covers what the map can’t: the parts of XML that JSON has no word for. This isn’t a migration in the usual sense. You don’t replace JsonSerializer with XmlSerializer; you use the one that matches the format on the wire. Many applications use both, with the same model types. To turn a document of one format into the other, see Convert between XML and JSON.

Start with what you know

The following code serializes and deserializes an object with options, the way you’d write it with JsonSerializer and JsonSerializerOptions.
CatalogItem has no attributes. It’s the kind of type you already have:
CatalogItem.cs
The sample fills it in like this:
Program.cs
The XmlEssentials code produces the following output:
Compare it with the JSON you’d expect, and four XML facts show up at once: the document starts with a declaration, the object has a name (catalogItem), the list is a repeated element with no brackets around it, and the null member is absent because of WhenWritingNull. The rest of this article explains each one. View the complete sample on GitHub

Type-for-type map

Every XmlEssentials type in these tables exists. The types are in CloudNimble.Text.Xml, CloudNimble.Text.Xml.Nodes, and CloudNimble.Text.Xml.Serialization, which correspond to System.Text.Json, System.Text.Json.Nodes, and System.Text.Json.Serialization.

Serializer

Attributes

Converters and metadata

Contract customization isn’t supported. XmlTypeInfo and XmlPropertyInfo describe a contract, but you can’t change one, and DefaultXmlTypeInfoResolver has no modifiers. To change how a type is written, use attributes, options, or a custom converter.

Reader and writer

Document models

What XML has that JSON doesn’t

A JSON document is a value: an object, an array, a string, a number, a Boolean, or null. An XML document is a tree of named elements that carry attributes and text. The following sections cover each place where that difference reaches the API.

A root element name

A JSON object has no name. An XML document is one element, and that element has one. By default it’s the type name, after the naming policy: CatalogItem became catalogItem in the first sample. [XmlRoot("name")] sets it. The name is checked when you deserialize. A document whose root element has another name is an XmlException, where System.Text.Json would read any object.

Attributes, elements, and text

A JSON property has one shape. A member of an XML type has three to choose from:
  • A child element, the default: <headline>Reading XML as UTF-8</headline>.
  • An attribute on the parent’s start tag, with [XmlAttribute]: id="12". Only for simple values.
  • The text of the parent element itself, with [XmlText]. This is how an element has both attributes and a value.
The following type uses all three, a namespace, and a wrapped list:
Article.cs
Program.cs
The code produces the following output:
To write simple members as attributes without annotating each one, set XmlSerializerOptions.DefaultPropertyMapping to XmlPropertyMapping.Attribute. See Customize names. View the complete sample on GitHub

No arrays

JSON has [ ]. XML has only elements, so a list is one of two conventions, and you choose:
  • Unwrapped, the default: one element for each item, named for the member. Tags in the first sample became two <tags> elements.
  • Wrapped, with [XmlArray]: one element for the list, and an element inside it for each item. Tags in Article became <tags> with two <tag> children.
One consequence has no JSON equivalent: an unwrapped list with no items writes nothing, so an empty list and a null list can’t be told apart when you read the document back. A wrapped list writes its wrapper element even when it’s empty. See Collections. A Dictionary<string, TValue> is written like a JSON object: one child element for each entry, named for the key.

Namespaces

An XML name has two parts: a local name and a namespace URI. Two elements named title in different namespaces are different elements, and a prefix such as atom: is only a local abbreviation for the URI. JSON has nothing like it. In the API, a namespace appears as:
  • Namespace on [XmlRoot], [XmlElement], [XmlAttribute], and [XmlArray], and DefaultNamespace on XmlSerializerOptions. The xmlns="urn:contoso:blog" in the last output came from [XmlRoot].
  • A second argument on the members that take a name: GetChild(localName, namespaceUri), WriteStartElement(localName, namespaceUri).
  • NamespaceUriEquals and NamespaceUriSpan on the reader, beside LocalNameEquals and LocalNameSpan.
Names are always matched by local name and URI, never by prefix. See Namespaces.

Element order

The order of the properties in a JSON object carries no meaning. The order of child elements in XML often does, because an XML Schema can require a sequence. XmlEssentials writes attributes first, then child elements in declaration order with base class members first, and [XmlPropertyOrder] sets an explicit order. On read, elements are matched by name in whatever order they arrive.

Null, nil, and empty

JSON has one way to say “no value”: null. XML has three shapes, and they mean different things:
  • The element is absent. The property keeps its default.
  • The element is present and nil: <Description xsi:nil="true"/>. The property is null.
  • The element is present and empty: <DisplayName></DisplayName> or <DisplayName/>. The property is an empty string.
The following code writes a null string and an empty string with the default options and reads them back:
Program.cs
The code produces the following output:
xsi:nil is the XML Schema way to write null, and it’s the default, which matches JsonIgnoreCondition.Never. XmlSerializerOptions.DefaultIgnoreCondition leaves a null member out, as JsonSerializerOptions.DefaultIgnoreCondition does. The empty Tags list is absent from this output for the reason given under No arrays. See Null and nil. View the complete sample on GitHub

No numbers or Booleans

JSON tells you the kind of a value: 3 is a number, true is a Boolean, "3" is a string. In XML, all three are text. <Count>3</Count> holds the characters 3, and nothing in the document says whether that’s a number. So there is no JsonValueKind.Number to test. The kind of an XML node tells you its structure (element, attribute, text), and you choose the type when you read:
Program.cs
The code produces the following output:
GetString and GetInt32 both succeed on the same element. The typed methods parse the lexical forms that XML Schema defines, which are culture-invariant: true, false, 1, and 0 for a Boolean, 2026-03-14T09:30:00Z for a date and time, P1DT12H for a TimeSpan. A value that isn’t in the form is an XmlException. The serializer uses the same forms, so the type of your property decides how the text is read. See Supported types. View the complete sample on GitHub

Mixed content

An XML element can hold text and child elements interleaved: <p>Read the <b>whole</b> page.</p>. JSON has no equivalent, and a plain class can’t hold it, because the order of the pieces matters. Map it with [XmlMixed], or read it with the document models, where an element’s children are text nodes and element nodes in order. See Mixed content.

The XML declaration

An XML document can begin with <?xml version="1.0" encoding="utf-8"?>. It’s written by default, by the serializer and by Utf8XmlWriter. Set OmitXmlDeclaration when you write a fragment that goes inside another document. XmlEssentials reads and writes UTF-8 only, so the encoding in the declaration is always utf-8.

Comments, CDATA, and processing instructions

XML has node kinds that JSON doesn’t: comments, CDATA sections (text that isn’t escaped), and processing instructions. The reader and the document models report them, and the writer writes them. By default, the serializer skips comments, reads a CDATA section as text, and writes a member as CDATA when it has [XmlCData].

What isn’t in XML

A few things you may reach for don’t apply:
  • JsonSerializerOptions.Encoder and relaxed escaping. XML escaping is fixed by the specification.
  • AllowTrailingCommas, AllowReadingFromString, WriteAsString, AllowOutOfOrderMetadataProperties. They have no XML meaning. A discriminator, a reference id, and a reference are XML attributes, and the order of attributes isn’t significant, so they are accepted in any position.
  • DTDs and custom entities. XmlEssentials rejects a document that has a <!DOCTYPE. See Security.
On the benchmarks page, XmlEssentials reads and writes objects at about the speed System.Text.Json does: of 47 comparisons it’s ahead in 10, level in 19, and behind in 18. It’s behind on single values and on the mutable tree, where XML takes more work to parse and write than JSON does. Choose the format for what’s on the wire, not for speed.

See also

Quickstart

Install the package and run the basics.

Serialize

Every way to write an object as XML.

Customize names

Element and attribute names, and which members become attributes.

Collections

Wrapped and unwrapped lists, and dictionaries.

Null and nil

Absent, nil, and empty.

Namespaces

Local names, URIs, and prefixes.

Use source generation

XmlSerializerContext for trimming and Native AOT.

Mapping to the System.Text.Xml proposal

Name by name against dotnet/runtime issue 125299.