Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueTypeSyntax
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:
-
Before the first block, create a state that holds your options:
-
For every block, pass the state to the reader’s constructor, and take the new state from
CurrentStatewhenReadreturnsfalse:
Options, the options it was created with. The complete loop is in the examples.
Remarks
The state is opaque. As with the state of aUtf8JsonReader, 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.Considerations
- A state made without options uses the defaults.
new XmlReaderState()anddefault(XmlReaderState)hold the default options, with which the reader reports comments and white space between elements. - Taking a state allocates.
Utf8XmlReader.CurrentStatecopies 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.JsonReaderStatehas 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
Readreturnsfalsein the middle of a token, the reader stops at the start of that token. The bytes afterBytesConsumedare 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:spacesetting 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 forMaxDocumentByteCount. - After
TrySkipreturnsfalse, 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 theReadthat 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.ValueTypeSyntax
.ctor Inherited
Inherited from
objectSyntax
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.ValueTypeSyntax
Parameters
Returns
Type:bool
Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Override
Inherited from
System.ValueTypeSyntax
Returns
Type:int
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
ToString Override
Inherited from
System.ValueTypeSyntax
Returns
Type:string?
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.Utf8XmlReader
- CloudNimble.Text.Xml.XmlReaderOptions
- CloudNimble.Text.Xml.XmlCommentHandling