Skip to content

About

The System.Text.Json equivalent for XML that .NET developers have been begging for

Topics

Resources

Stars

24 stars

Watchers

0 watching

Forks

Repository files navigation

XmlEssentials

UTF-8 XML reader, writer, DOM, and AOT-safe serializer for .NET with a System.Text.Json-shaped API. Zero System.Xml dependency.

NuGet version of XmlEssentials Total NuGet downloads of XmlEssentials Build and Deploy workflow status on main License: MIT

Package XmlEssentials
Assembly CloudNimble.XmlEssentials
Namespace CloudNimble.Text.Xml
Target frameworks net10.0, net11.0
Dependencies None

What it does

Each part of XmlEssentials has a System.Text.Json counterpart with the same shape:

To Use The System.Text.Json counterpart
Turn objects into XML and back XmlSerializer JsonSerializer
Read XML one token at a time Utf8XmlReader Utf8JsonReader
Write XML without building objects first Utf8XmlWriter Utf8JsonWriter
Look up values in a document you don't change XmlDocument JsonDocument
Build or edit a document in memory XmlNode JsonNode
  • No System.Xml. The runtime library doesn't reference System.Xml or System.Private.Xml, and a test fails if it ever does.
  • Native AOT and trimming. The package includes a Roslyn source generator. A partial class that derives from XmlSerializerContext and names your types with [XmlSerializable<T>] gets their serialization code at build time. No trimming or AOT warning is suppressed in the library; the reflection-based overloads are marked [RequiresUnreferencedCode] and [RequiresDynamicCode].
  • UTF-8 throughout. Every entry point takes UTF-8 bytes, spans, sequences, and streams. The string overloads convert first.
  • Standards. Well-formed XML 1.0 (Fifth Edition) and XML 1.1 (Second Edition), with Namespaces in XML 1.0 and 1.1. The tests run cases from the W3C XML Conformance Test Suite.
  • Untrusted input. A document with a <!DOCTYPE is rejected, so entity expansion and external entities can't happen. Depth, attribute-count, and size limits are on by default.
  • Existing XML formats. The Bcl preset of XmlSerializerOptions reads and writes what System.Xml.Serialization.XmlSerializer produces for the same types.

Built for the future

XmlEssentials targets .NET 10 and .NET 11 and nothing older. That means a batter developer experience that doesn't make you luck into success.

  • Generic attributes. [XmlSerializable<T>], [XmlDerivedType<T>], [XmlConverter<TConverter>], [XmlElement<T>], and [XmlArrayItem<T>] take the type as a type argument. The compiler checks it, and there is no typeof.
  • Closed types. On .NET 11, InferClosedTypePolymorphism takes the derived types of a closed hierarchy from the compiler, so you register none.
  • Modern type shapes. Positional records, constructor parameters, and init and required members are read and written as you declare them.
  • Async and streaming. Stream, PipeReader and PipeWriter, and IAsyncEnumerable<T>, each with a CancellationToken. Utf8XmlWriter is IAsyncDisposable.
  • Spans and buffers. ReadOnlySpan<byte>, ReadOnlySequence<byte>, and IBufferWriter<byte> throughout. Utf8XmlReader is a ref struct.
  • Today's value types. DateOnly, TimeOnly, Int128, UInt128, and Half are supported without a converter.
  • No reflection in generated code. A generated context reaches init, required, and non-public members through [UnsafeAccessor].

Install

dotnet add package XmlEssentials

Or in your project file:

<ItemGroup>
  <PackageReference Include="XmlEssentials" Version="1.*" />
</ItemGroup>

The one package contains the library and its source generator. It needs .NET 10 or .NET 11.

Quickstart

Create a console app with dotnet new console, add the package, and replace Program.cs with:

using CloudNimble.Text.Xml;

var person = new Person { Name = "Ada", Age = 36 };

string xml = XmlSerializer.Serialize(person);
Console.WriteLine(xml);

Person? copy = XmlSerializer.Deserialize<Person>(xml);
Console.WriteLine($"{copy!.Name} is {copy.Age}");

public sealed class Person
{
    public string? Name { get; set; }

    public int Age { get; set; }
}

Output:

<?xml version="1.0" encoding="utf-8"?><Person><Name>Ada</Name><Age>36</Age></Person>
Ada is 36

Public properties with a getter and a setter become child elements, in the order you declare them. If a file also imports System.Xml.Serialization, the name XmlSerializer is ambiguous, so remove the System.Xml imports from files you move over.

Usage

Generate the serializer at build time

Reflection is the default because it needs no setup. For Native AOT or trimmed apps, declare a context and pass its type info:

using CloudNimble.Text.Xml;
using CloudNimble.Text.Xml.Serialization;

string xml = XmlSerializer.Serialize(person, PersonXmlContext.Default.Person);
Person? copy = XmlSerializer.Deserialize(xml, PersonXmlContext.Default.Person);

[XmlSerializable<Person>]
public partial class PersonXmlContext : XmlSerializerContext
{
}

Read tokens from UTF-8

ReadOnlySpan<byte> utf8 = "<Order id=\"1042\"><Item>Keyboard</Item><Item>Mouse</Item></Order>"u8;

var reader = new Utf8XmlReader(utf8);
while (reader.Read())
{
    if (reader.TokenType is XmlTokenType.StartElement && reader.LocalNameEquals("Item"u8))
    {
        reader.Read();
        Console.WriteLine(reader.GetString()); // Keyboard, then Mouse
    }
}

Query a parsed document

using XmlDocument document = XmlDocument.Parse("""<Order id="1042"><Total>129.90</Total></Order>""");
XmlElement order = document.RootElement;

Console.WriteLine($"Order {order.GetAttribute("id").GetInt32()}: {order.GetChild("Total").GetDecimal()}");
// Order 1042: 129.90

Dispose every XmlDocument: its memory is rented from a pool. Create XmlSerializerOptions once and reuse it, because each instance caches what it learns about your types.

Performance

Across 67 like-for-like benchmarks against System.Xml, XmlEssentials is 3.0× faster on average and allocates 85% less memory. It wins 66 of the 67.

Operation System.Xml XmlEssentials Faster
Read an int 1.07 µs / 5,968 B 56 ns / 0 B 19×
Read a customer (object graph) 8.75 µs / 16,496 B 2.67 µs / 3,168 B 3.3×
Write 1,000 orders 425.1 µs / 214,508 B 201.1 µs / 0 B 2.1×
Parse a 198 KB RSS feed into a DOM 345.4 µs / 709,083 B 108.8 µs / 105 B 3.2×
Read a 198 KB RSS feed, token by token 191.9 µs / 47,137 B 82.4 µs / 1,441 B 2.3×
Write a 200-item catalog 33.4 µs / 13,728 B 16.4 µs / 0 B 2.0×

Time / allocated bytes for one operation. One BenchmarkDotNet 0.15.8 run of 229 benchmarks on 2026-11-25: .NET 11 on x64 with the JIT, Windows 11, Intel Core Ultra 9 285K under Hyper-V. Native AOT speed isn't measured. On objects it runs level with System.Text.Json; on single values and the mutable tree it is behind. Every row, the method, and the one comparison System.Xml wins are on the benchmarks page, and the benchmark project is in src/CloudNimble.XmlEssentials.Performance.

Limitations

  • UTF-8 only. Documents in other encodings aren't read.
  • No DTD processing, and no setting turns it on.
  • Generating types from an .xsd file is a preview feature and doesn't cover all of XML Schema.

Resources

License

XmlEssentials is released under the MIT License. Copyright © 2026 CloudNimble, Inc.

About

The System.Text.Json equivalent for XML that .NET developers have been begging for

Topics

Resources

Stars

24 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages