Skip to main content

Summary

CloudNimble.Text.Xml is the main namespace of XmlEssentials. It holds the four ways to work with XML, all of them over UTF-8 and all of them shaped like their counterparts in System.Text.Json: The tree that you can change, XmlNode, is in CloudNimble.Text.Xml.Nodes. Attributes, converters, and source generation support are in CloudNimble.Text.Xml.Serialization. Nothing in the library depends on System.Xml.

Usage

Install the XmlEssentials package and import the namespace:
Then choose the entry point that fits the job. Start at the top of this list and move down only when you need to.

XmlSerializer: your types to XML and back

XmlSerializer.Serialize and XmlSerializer.Deserialize convert between .NET objects and XML. This is the entry point for most applications.
See How to serialize and How to deserialize.

XmlDocument: a read-only tree

XmlDocument.Parse reads a whole document into pooled memory. RootElement and the XmlElement values you get from it let you move through the document in any order.
See How to use XmlDocument. To change a document, use the tree in CloudNimble.Text.Xml.Nodes, described in How to use XmlNode.

Utf8XmlReader: one token at a time

Utf8XmlReader reads UTF-8 bytes forward, one token per call to Read, and gives you names and values as slices of your input.
The example reads a fragment with two top-level elements, which is why it sets AllowMultipleRoots. See How to read XML with Utf8XmlReader.

Utf8XmlWriter: one call at a time

Utf8XmlWriter writes UTF-8 to an IBufferWriter<byte> or a Stream. It escapes values, declares namespaces, and checks that the output is well formed.
See How to write XML with Utf8XmlWriter.

Examples

Each example is a sample in the repository whose output is checked by a test.

Serialize and deserialize an object

View the complete sample on GitHub

Read a document with Utf8XmlReader

View the complete sample on GitHub

Write a document with Utf8XmlWriter

View the complete sample on GitHub

Query a document with XmlDocument

View the complete sample on GitHub

Best Practices

  • Start with the highest-level API that does the job. Use XmlSerializer for your own types, XmlDocument to look inside a document whose shape you don’t control, and the reader and writer when you need to stream or to control every byte.
  • Keep XML as UTF-8. Every type in this namespace works on UTF-8 bytes. Read and write byte[], spans, streams, and u8 literals, and convert to string only at the edges of your application.
  • Know which defaults keep everything. Utf8XmlReader and XmlDocument report comments and the white space between elements by default, so that XML that’s read and written back is unchanged. XmlSerializer skips both. In a hand-written read loop that only wants data, set CommentHandling to Skip and WhitespaceHandling to Insignificant.
  • Dispose what’s disposable. XmlDocument returns pooled memory when you dispose it, and Utf8XmlWriter flushes when you dispose it.
  • Leave the limits on for input you don’t trust. A limit of 0 selects a safe default, not “no limit”. See Security.
  • Catch CloudNimble.Text.Xml.XmlException. It isn’t related to System.Xml.XmlException.

Patterns

The four entry points are built on each other, and you can move between them:
  • The serializer uses the reader and the writer. A custom converter receives a Utf8XmlReader to read its value and a Utf8XmlWriter to write it.
  • A document writes itself to a writer. XmlDocument.WriteTo and XmlElement.WriteTo copy a parsed document, or one element of it, into a Utf8XmlWriter. See How to use XmlDocument.
  • The serializer reads from and writes to the tree. You can deserialize from an XmlDocument, an XmlElement, or an XmlNode, and serialize to them. See How to deserialize.
  • Reader in, writer out. To transform a large document without holding it in memory, read tokens with Utf8XmlReader and write what you keep with Utf8XmlWriter. See How to read XML with Utf8XmlReader.

Considerations

  • UTF-8 only. A document that declares another encoding is rejected with an XmlException. Transcode it first.
  • No DTDs. A document type declaration is rejected, so a document can’t define entities. Only the five predefined entities and character references are decoded. See Security.
  • XML 1.0 and XML 1.1. The reader takes the version from the XML declaration and applies that version’s rules. The writer writes the version you set in XmlWriterOptions.Version.
  • Namespaces are always processed. Names are reported as a local name and a namespace URI. An element without a prefix is in the default namespace. An attribute without a prefix is in no namespace.
  • Empty elements have no end token. Utf8XmlReader reports <e/> as one StartElement token with IsEmptyElement set.
  • Typed values use XML Schema forms. Numbers, dates, durations, and Booleans are read and written in their XML Schema lexical forms with the invariant culture.
  • The names match System.Xml in places, the types don’t. XmlDocument, XmlElement, XmlSerializer, and XmlException in this namespace are different types from those in System.Xml. Avoid importing both namespaces in one file, or use an alias.
  • Thread safety differs by type. See Thread safety.
For what the library accepts and rejects, see XML conformance.
  • CloudNimble.Text.Xml.XmlSerializer
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.XmlElement
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlException
  • CloudNimble.Text.Xml.Nodes.XmlNode

Types

Classes

Structs

Enums