Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueType

Syntax

Summary

Provides options that control how a Utf8XmlReader reads XML.

Usage

Create the options, set the properties you need, and pass the value to a Utf8XmlReader constructor. The reader copies the options, so later changes to your variable don’t affect it. The most common use is a reader that reports data only, without comments and without the white space between elements:
The properties fall into three groups: When you read in blocks, the options travel in the XmlReaderState: new XmlReaderState(options).

Remarks

A limit property set to 0 uses the default limit stated on that property, except XmlReaderOptions.MaxDocumentByteCount, where 0 means no limit. default and new XmlReaderOptions() are the same options. By default, the reader reports comments and whitespace-only text between elements, so that XML that is read and written back keeps its comments and its layout. For a read loop that wants data only, set XmlReaderOptions.CommentHandling to XmlCommentHandling.Skip and XmlReaderOptions.WhitespaceHandling to XmlWhitespaceHandling.Insignificant.

Examples

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

Set limits

Each call reads the same document with one limit lowered. The last call uses the defaults.
View the complete sample on GitHub

Read a fragment

View the complete sample on GitHub

Best Practices

  • Set CommentHandling to Skip and WhitespaceHandling to Insignificant when your code only wants data. The defaults report comments and the white space between elements, which is what code that copies or edits a document needs.
  • Leave the limits at their defaults unless you have a reason. The defaults already protect against documents that nest too deep, carry too many attributes, or hold a name or a value that’s too large.
  • Set MaxDocumentByteCount for input you don’t trust. It’s the one limit that’s off by default.
  • Lower MaxDepth when you know the shape of your documents. A document format that nests five levels has no need for 64.
  • Use XmlCommentHandling.Disallow and XmlWhitespaceHandling.None only for a strict contract between programs, where a comment or indentation means that the producer is wrong.
See Security for how the limits fit together.

Considerations

Know the following about the defaults and the limits.

The defaults keep everything

new XmlReaderOptions() and default(XmlReaderOptions) are the same value. CommentHandling is Allow and WhitespaceHandling is Significant, so the reader reports comments as Comment tokens and white-space-only text between elements as Text tokens. The XML specification asks a processor to pass all of a document’s characters to the application, and XML that’s read and written back then keeps its comments and layout. XmlSerializerOptions has the opposite defaults, because neither belongs in an object.

A limit of 0 means the default

MaxDocumentByteCount is the exception: 0 means no limit. The reader checks it in its constructor, against the bytes read in earlier blocks plus the length of the block it’s given.

What the limits count

  • MaxAttributeCount includes namespace declarations.
  • MaxNameByteCount, MaxTextByteCount, and MaxCommentByteCount are in UTF-8 bytes, not characters, counted as they are in the document, before references are expanded.
  • MaxTextByteCount applies to each text node, CDATA section, attribute value, and processing instruction’s data on its own.
  • When the document is read in blocks, a token that is already longer than its limit throws before its end arrives.
  • MaxCommentByteCount applies to comments that are skipped as well as to comments that are reported.
  • Exceeding a limit throws an XmlException whose message names the property and the limit, such as MaxDepth exceeded (64).

Where else the options apply

These options are for Utf8XmlReader. XmlDocument and XmlSerializer have their own options types, XmlDocumentOptions and XmlSerializerOptions.

Constructors

.ctor

Initializes a new instance of the XmlReaderOptions struct.

Syntax

Remarks

The result is the same as default: XmlReaderOptions.CommentHandling is XmlCommentHandling.Allow, XmlReaderOptions.WhitespaceHandling is XmlWhitespaceHandling.Significant, and every limit property is 0, which selects its default limit.

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

AllowMultipleRoots

Gets or sets a value that indicates whether the reader accepts a document fragment, which can have more than one top-level element or none.

Syntax

Property Value

Type: bool true if more than one top-level element, or none, is allowed; otherwise, false. The default is false.

CommentHandling

Gets or sets a value that defines how the reader handles comments.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlCommentHandling One of the enumeration values that indicates how comments are handled. The default is XmlCommentHandling.Allow.

Remarks

Comments are reported by default so that XML that is read and written back does not lose them. Set this property to XmlCommentHandling.Skip when your code does not want to see XmlTokenType.Comment tokens.

MaxAttributeCount

Gets or sets the maximum number of attributes allowed on one element, including namespace declarations.

Syntax

Property Value

Type: int The maximum number of attributes on one element. The default is 0, which means a maximum of 1000.

MaxCommentByteCount

Gets or sets the maximum size of the content of a comment.

Syntax

Property Value

Type: int The maximum size of a comment’s content, in UTF-8 bytes. The default is 0, which means a maximum of 1,048,576.

MaxDepth

Gets or sets the maximum depth of nested elements allowed when reading XML.

Syntax

Property Value

Type: int The maximum element depth. The default is 0, which means a maximum depth of 64.

MaxDocumentByteCount

Gets or sets the maximum size of the document.

Syntax

Property Value

Type: int The maximum size of the document, in bytes. The default is 0, which means no limit.

MaxNameByteCount

Gets or sets the maximum length of an element or attribute name.

Syntax

Property Value

Type: int The maximum length of a name, in UTF-8 bytes. The default is 0, which means a maximum of 1024.

MaxTextByteCount

Gets or sets the maximum length of a text node, a CDATA section, an attribute value, or the data of a processing instruction.

Syntax

Property Value

Type: int The maximum length of each, in UTF-8 bytes as they appear in the document. The default is 0, which means a maximum of 16,777,216.

WhitespaceHandling

Gets or sets a value that defines how the reader handles whitespace-only text between elements.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlWhitespaceHandling One of the enumeration values that indicates how whitespace-only text is handled. The default is XmlWhitespaceHandling.Significant.

Remarks

Whitespace-only text between elements is reported by default so that XML that is read and written back does not change its layout. Set this property to XmlWhitespaceHandling.Insignificant when your code does not want to see those XmlTokenType.Text tokens.

Methods

Equals Override

Inherited from System.ValueType

Syntax

Parameters

Returns

Type: bool

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetHashCode Override

Inherited from System.ValueType

Syntax

Returns

Type: int

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Override

Inherited from System.ValueType

Syntax

Returns

Type: string?

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.XmlReaderState
  • CloudNimble.Text.Xml.XmlCommentHandling
  • CloudNimble.Text.Xml.XmlWhitespaceHandling
  • CloudNimble.Text.Xml.XmlDocumentOptions
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlException