Prerequisites
- .NET 10 or .NET 11. The package targets
net10.0andnet11.0. - A project to add the package to.
dotnet new consoleis enough.
Install the package
1
Add the package to your project
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
Program.cs
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.
"..."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.
&. 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 anXmlDocument. 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.
Change a document
To edit XML in memory, use the node tree inCloudNimble.Text.Xml.Nodes. It’s the counterpart of JsonNode.
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
stringoverloads are there for convenience, and they convert first. - Create
XmlSerializerOptionsonce 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.