Utf8XmlReader applies every rule on this page. XmlDocument, XmlNode, and XmlSerializer read through Utf8XmlReader and write through Utf8XmlWriter, so the same rules hold for them.
Specifications
XmlEssentials is a non-validating processor that doesn’t read the DTD. The XML specification requires a processor to at least read the internal DTD subset, so this is a deliberate departure, made for security. A document that has a
<!DOCTYPE is rejected. It isn’t read with the DTD skipped.
Conformance tests
The test project runs cases from the W3C XML Conformance Test Suite (xmlts20130923) in XmlConformanceSuiteTests. The test reads each W3C manifest and checks every case whose document is present against the result the manifest states.
The cases come from these manifests:
For the XML 1.0 sections, only the documents that this library can judge were taken. Nearly all the others need a DTD.
For each case that is judged on well-formedness:
- A document the manifest marks
not-wfmust throw fromUtf8XmlReaderand must failXmlDocument.TryParse. - A document marked
validorinvalidmust be read. Validity is a DTD notion, and this isn’t a validating parser. - A document that is read must produce the same tokens when the input arrives in 1-byte and in 3-byte pieces, and the same tokens again after it is parsed into an
XmlDocumentand written back.
System.Xml.XmlReader.
Encoding
Input and output are UTF-8.The declared encoding is checked for bytes only. A .NET
string or a span of characters has no encoding of its own, so XmlSerializer.Deserialize, XmlDocument.Parse, and XmlNode.Parse read a string that starts with <?xml version="1.0" encoding="utf-16"?>, which is what System.Xml writes to a StringWriter.<?xml version="1.0" encoding="utf-8"?>, or version="1.1" when XmlWriterOptions.Version is XmlVersion.Version11, and adds standalone="yes" or standalone="no" when XmlWriterOptions.Standalone is set. It writes a byte order mark only when EmitByteOrderMark is true. To convert a document in another encoding, transcode it to UTF-8 first, for example with Encoding.CreateTranscodingStream, and fix the declaration.
The XML declaration
The declaration must match production 23 of the specification:version first and required, then optional encoding, then optional standalone with the value yes or no. It must be the first thing in the document, after an optional byte order mark.
Line ends
The reader replaces every line end with one line feed (U+000A) before it reports a value, as section 2.11 of the specification requires. XML 1.1 has more line ends than XML 1.0.
The following sample reads the same text in both versions.
ReadText returns the first text token, with control characters replaced by their C# escapes so that you can see them:
XmlException.LineNumber counts lines after this step. XmlException.LinePosition is the one-based offset in the line in UTF-8 bytes, not in characters.
A line end that is written as a character reference, such as 
, isn’t a line end in the document. It is read as the character it names.
Attribute values
Because there is no DTD, every attribute is treated as character data (CDATA), and its value is normalized as section 3.3.3 of the specification describes:
- Line ends are normalized as above.
- Each tab, line feed, and carriage return that is written literally becomes one space.
- Character references and the predefined entities are expanded. A tab or line end written as a reference stays what it is.

 is still a line feed.
Whitespace
Whitespace inside an element that has other text is always part of the value: the text of<item> a </item> is " a ". What you choose is how the reader treats text that is only whitespace and sits between elements, which is usually indentation. Set WhitespaceHandling on XmlReaderOptions, XmlDocumentOptions, or XmlSerializerOptions.
Whitespace-only text is indentation only when it carries no content. Under every setting it’s reported as a
Text token when it’s the whole content of its element (<item> </item> is " "), when its element already has text or a CDATA section before it (the space between </b> and <i> in <p>a <b>b</b> <i>c</i></p>), or when xml:space="preserve" is in force. xml:space="preserve" applies to the element that has it and to everything inside, until an element sets xml:space="default" (section 2.10).
The default depends on the API:
default and new give the same options for the reader and for the document.
ReadTokens reads a whole document and lists its tokens, or returns the error that stopped the reader:
Indented is true, and only between elements. An element that holds text is written on one line, so indentation never changes a value. AttributeStyle = XmlAttributeStyle.PerLine adds line breaks inside start tags, between attributes, where whitespace isn’t content.
Comments and processing instructions
Comments and processing instructions are read inside the root element and before and after it. A processing instruction is always reported as a token. How comments are treated depends onXmlCommentHandling, and the default depends on the API:
Character and entity references
The reader expands the five predefined entities and numeric character references, in text and in attribute values.HTML entities such as
are defined by a DTD, so they aren’t available. Use the character itself or a numeric reference such as  .What isn’t supported
These are the deliberate departures from the specifications. Each one is an error. Nothing is skipped silently.CDATA (so no ID or NMTOKENS normalization), and the standalone checks. The standalone value is read and reported, and has no other effect.
Well-formedness errors
The reader throws anXmlException for a document that isn’t well formed. These are the messages for the errors you are most likely to meet:
An end tag must repeat the prefix of its start tag.
<a:e xmlns:a="urn:u" xmlns:b="urn:u"></b:e> is an error although both prefixes are bound to the same namespace (XML 1.0 section 3). Everywhere else, a name is its local name and namespace URI, and the prefix doesn’t matter.
Two attributes are duplicates when they have the same local name and the same namespace URI, even with different prefixes.
The reader also enforces limits on depth, attribute count, and the size of names, text, and comments. See Security.
XML 1.1 differences
XML 1.1 isn’t a superset of XML 1.0. The reader and writer apply the rules of the version the document declares.XmlWriterOptions.Version or XmlSerializerOptions.XmlVersion to XmlVersion.Version11. System.Xml doesn’t read XML 1.1 documents.
Namespaces
For how to read and write namespaces, see Work with namespaces.
What the writer escapes
Reading normalizes values, so the writer protects everything that normalization would change. Any string the writer accepts as a value is returned unchanged by the reader.
Attribute values are always written in double quotation marks.
Two things switch these checks off:
XmlWriterOptions.SkipValidation, and WriteRawValue, which copies its input to the output without checking it. See Use Utf8XmlWriter.
A lone UTF-16 surrogate in a .NET string isn’t a character. The writer throws XmlException with the message The text contains half of a surrogate pair.
Round trips through XmlDocument
With its default options,XmlDocument.Parse keeps comments and the whitespace-only text between elements, and XmlDocument.WriteTo writes the document it parsed with the same elements, attributes, namespace declarations, comments, processing instructions, and text, in the same order. An empty element (<e/>) and a start tag followed by an end tag (<e></e>) stay as they were, unless XmlWriterOptions.EmptyElementStyle is StartAndEndTag, which writes both as <e></e>.
The output isn’t always the same bytes as the input. These details of the original text carry no information in XML, and they aren’t kept:
See Use XmlDocument.
See also
Security
Limits, DTD rejection, and guidance for untrusted input.
Supported types
The text each .NET type is written as and read from.
Use Utf8XmlReader
Read tokens, values, and errors.
Use Utf8XmlWriter
Write elements, attributes, and values.