Skip to main content
In this quickstart, you install XmlEssentials and use each of its four entry points once: the serializer, the reader, the writer, and the document object model. Every sample on this page is compiled and tested in the repository, and each one links to its source.

Prerequisites

  • .NET 10 or .NET 11. The package targets net10.0 and net11.0.
  • A project to add the package to. dotnet new console is enough.

Install the package

1

Add the package to your project

The one package contains the library and its source generator. There’s nothing else to install.
2

Import the namespaces

CloudNimble.Text.Xml holds the serializer, reader, writer, and document. CloudNimble.Text.Xml.Serialization holds the attributes, converters, and source-generation types.
XmlEssentials doesn’t use System.Xml. If a file imports both System.Xml.Serialization and CloudNimble.Text.Xml, the name XmlSerializer is ambiguous. Remove the System.Xml imports from files you move over. See Migrate from XmlSerializer.

Serialize and deserialize an object

XmlSerializer turns objects into XML and back. If you’ve used JsonSerializer from System.Text.Json, you already know the calls. Start with a class. Public properties with a getter and a setter are written as child elements, in the order you declare them:
Person.cs
Then serialize it and read it back:
Program.cs
The code produces the following output:
View the complete sample on GitHub
In these samples, output is a TextWriter. In your own code, use Console.Out or call Console.WriteLine.
From here, you shape the XML with attributes and options: customize names, write properties as XML attributes, format the output, or map collections.

Read XML one token at a time

Utf8XmlReader reads UTF-8 bytes directly and hands you one token per call to Read. It doesn’t allocate for the tokens it reads, which makes it the right tool for large documents and hot paths. It’s the counterpart of Utf8JsonReader.
The code produces the following output:
View the complete sample on GitHub The "..."u8 literals are UTF-8 strings, so names are compared as bytes and nothing is converted to a string until you call GetString. For attributes, chunked input, namespaces, and limits, see Use Utf8XmlReader.
By default, the reader also reports comments and the white space between elements, so that XML you read and write back keeps its comments and its layout. This loop ignores them because it acts only on start tags. For a loop that wants data only, pass new XmlReaderOptions { CommentHandling = XmlCommentHandling.Skip, WhitespaceHandling = XmlWhitespaceHandling.Insignificant }.

Write XML

Utf8XmlWriter writes UTF-8 straight into a buffer or a stream, and it escapes text for you. It’s the counterpart of Utf8JsonWriter.
The code produces the following output:
View the complete sample on GitHub The writer added the XML declaration and escaped the &. For namespaces, reusing a writer, and writing to streams, see Use Utf8XmlWriter.

Query a document

When you want to look things up instead of reading front to back, parse the XML into an XmlDocument. It’s read-only and keeps its data in pooled buffers, so parsing a document of any size allocates one small object. It’s the counterpart of JsonDocument. A document keeps comments and the white space between elements, so that writing it back with WriteTo doesn’t lose them. That’s why the loop below checks ValueKind: the indentation between the items is there too, as text.
The code produces the following output:
View the complete sample on GitHub
Always dispose an XmlDocument, with using or a call to Dispose. Disposing returns its buffers to the pool. An XmlElement is a view into the document, so don’t use one after the document is disposed.
For more, see Use XmlDocument.

Change a document

To edit XML in memory, use the node tree in CloudNimble.Text.Xml.Nodes. It’s the counterpart of JsonNode.
The code produces the following output:
View the complete sample on GitHub For more, see Use XmlNode.

Choose the right entry point

Best practices from the start

  • Work in UTF-8 when you can. Every entry point accepts UTF-8 bytes, spans, and streams. The string overloads are there for convenience, and they convert first.
  • Create XmlSerializerOptions once and reuse it. An options instance caches what it learns about your types. A new instance for every call throws that work away. See Configure options.
  • Dispose every XmlDocument. Its memory is rented, not allocated.
  • Use a source-generated context when you publish with Native AOT or trimming. Reflection is the default because it needs no setup. Source generation removes it.
  • Set limits when the XML comes from someone you don’t trust. XmlEssentials doesn’t process DTDs or external entities, and the reader, document, and serializer all take size and depth limits. See Security.

Next steps

How to serialize

Strings, bytes, streams, indentation, and the declaration.

Shape your XML

Elements, attributes, text content, names, and order.

Use source generation

Serialize without reflection, ready for Native AOT.

Migrate from System.Xml

Move an existing XmlSerializer codebase across.