Skip to main content

Definition

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

Syntax

Summary

Represents the state a Utf8XmlReader needs to continue reading a document with more data.

Usage

XmlReaderState is what lets a Utf8XmlReader read a document that arrives in blocks. A reader is a ref struct and can’t outlive the block it reads, so everything it knows about the document so far travels in this value from one reader to the next. It’s the counterpart of JsonReaderState. You use it in two places:
  1. Before the first block, create a state that holds your options:
  2. For every block, pass the state to the reader’s constructor, and take the new state from CurrentState when Read returns false:
The state is opaque. Its only public member is Options, the options it was created with. The complete loop is in the examples.

Remarks

The state is opaque. As with the state of a Utf8JsonReader, only XmlReaderState.Options is visible. Utf8XmlReader.CurrentState copies an immutable snapshot so two readers never share mutable element or namespace stacks.

Examples

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

Read a stream in blocks

The loop reads a block from a stream, gives it to a reader with the state of the previous reader, and keeps the bytes that the reader didn’t consume for the next pass. The buffer is 16 bytes so that the sample has to refill it and grow it.
View the complete sample on GitHub How to read XML with Utf8XmlReader explains each step.

Considerations

  • A state made without options uses the defaults. new XmlReaderState() and default(XmlReaderState) hold the default options, with which the reader reports comments and white space between elements.
  • Taking a state allocates. Utf8XmlReader.CurrentState copies the names of the open elements and the namespace bindings in scope, because the buffer they point into is about to be reused. The cost grows with the depth of the document and the number of namespaces in scope. JsonReaderState has no such cost. Take the state once per block, not once per token.
  • A state is a snapshot. It never changes after you take it, and two readers created from the same state don’t affect each other. You can keep a state and go back to it, as long as you still have the bytes that follow it.
  • A state doesn’t hold bytes. When Read returns false in the middle of a token, the reader stops at the start of that token. The bytes after BytesConsumed are yours to keep and pass again, followed by the new data.
  • A state holds what the next reader needs to read the same way. That’s the open elements and the namespaces in scope, the xml:space setting and whether each open element already has text (so white space is reported as it would be in one piece), the XML version, the line count, and the number of bytes read for MaxDocumentByteCount.
  • After TrySkip returns false, the state is taken just after the start tag. A reader created from it reads the element’s content token by token. To skip the element whole in the next block, take the state from a copy of the reader made before the Read that reached the start tag. See Skip elements.
  • The options can’t change mid-document. The state carries them, and every reader created from the state uses them.
  • What your code knows isn’t in the state. Which element you’re inside, and what you’ve collected so far, belong in your own variables.

Constructors

.ctor

Initializes a new instance of the XmlReaderState struct using the specified options.

Syntax

Parameters

Remarks

The new state holds no read progress, so a reader created from it starts at the beginning of a document.

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

Options

Gets the custom behavior of the Utf8XmlReader that this state belongs to.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlReaderOptions The options the reader was created with.

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.XmlReaderOptions
  • CloudNimble.Text.Xml.XmlCommentHandling