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 theXmlEssentials package and import the namespace:
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.
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.
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.
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.
Examples
Each example is a sample in the repository whose output is checked by a test.Serialize and deserialize an object
Read a document with Utf8XmlReader
Write a document with Utf8XmlWriter
Query a document with XmlDocument
Best Practices
- Start with the highest-level API that does the job. Use
XmlSerializerfor your own types,XmlDocumentto 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, andu8literals, and convert tostringonly at the edges of your application. - Know which defaults keep everything.
Utf8XmlReaderandXmlDocumentreport comments and the white space between elements by default, so that XML that’s read and written back is unchanged.XmlSerializerskips both. In a hand-written read loop that only wants data, setCommentHandlingtoSkipandWhitespaceHandlingtoInsignificant. - Dispose what’s disposable.
XmlDocumentreturns pooled memory when you dispose it, andUtf8XmlWriterflushes 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 toSystem.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
Utf8XmlReaderto read its value and aUtf8XmlWriterto write it. - A document writes itself to a writer.
XmlDocument.WriteToandXmlElement.WriteTocopy a parsed document, or one element of it, into aUtf8XmlWriter. See How to use XmlDocument. - The serializer reads from and writes to the tree. You can deserialize from an
XmlDocument, anXmlElement, or anXmlNode, 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
Utf8XmlReaderand write what you keep withUtf8XmlWriter. 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.
Utf8XmlReaderreports<e/>as oneStartElementtoken withIsEmptyElementset. - 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.Xmlin places, the types don’t.XmlDocument,XmlElement,XmlSerializer, andXmlExceptionin this namespace are different types from those inSystem.Xml. Avoid importing both namespaces in one file, or use an alias. - Thread safety differs by type. See Thread safety.
Related APIs
- 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