Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml

Syntax

Summary

Provides a forward-only, non-cached writer for UTF-8 encoded XML text. This class cannot be inherited.

Usage

Create a writer over an IBufferWriter<byte> or a Stream, write the document with one call per start tag, attribute, value, and end tag, and dispose the writer to flush it.
View the complete sample on GitHub The members you use most: Most methods take names and values as a string, a ReadOnlySpan<char>, or UTF-8 in a ReadOnlySpan<byte>, and element names also as an XmlEncodedText. Pass an XmlWriterOptions to the constructor to indent, to leave out the declaration, or to change a limit. For a walk through every feature, see How to write XML with Utf8XmlWriter.

Remarks

The API follows the shape of Utf8JsonWriter. The writer writes to an IBufferWriter`1 or to a Stream, and the output is always UTF-8. Names and values can be passed as strings, as spans of characters, or as spans of UTF-8 bytes. A limit on XmlWriterOptions that is 0 uses its default: 64 for XmlWriterOptions.MaxDepth, 1000 for XmlWriterOptions.MaxAttributeCount, and 1024 for XmlWriterOptions.MaxNameByteCount. Utf8XmlWriter.Dispose and Utf8XmlWriter.DisposeAsync flush the output. If elements are still open and XmlWriterOptions.SkipValidation is false, they then throw an XmlException, so the output written so far is not lost with the error.

Examples

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

Write complete elements

View the complete sample on GitHub

Write namespaces

Pass the namespace URI, and the writer chooses or declares the prefix.
View the complete sample on GitHub

Reuse one writer for several documents

View the complete sample on GitHub

Best Practices

  • Dispose the writer, or call Flush, before you read the output. Until then, bytes are pending and the buffer or stream doesn’t have them.
  • End every element yourself. Dispose throws an XmlException when an element is still open. It doesn’t close elements for you.
  • Use the WriteElement* methods for elements that hold one value. One call replaces three.
  • Pass u8 literals for names that are fixed. writer.WriteStartElement("Order"u8) skips the conversion from UTF-16. For a name computed once at run time, keep an XmlEncodedText.
  • Declare the prefixes you use often on the root element with WriteNamespaceDeclaration. Otherwise the writer declares a prefix again on every element that needs it and isn’t inside one that has it.
  • Reuse a writer with Reset on a hot path. A reused writer allocates nothing per document. Call Flush before Reset, because Reset discards pending bytes.
  • Set NewLine when the bytes matter. The default depends on the operating system.
  • Write binary data that’s in one buffer with one WriteBase64StringValue call. Use WriteBase64StringSegment only when the data arrives in pieces.
  • Use WriteRawValue only for XML that your own code produced. The input isn’t escaped. By default the writer checks that it’s well formed where it goes, but well-formed input can still carry markup you didn’t intend.
  • Discard the output when the writer throws. Part of the document may already be committed.

Patterns

These are the shapes that code using the writer takes most often.

Element with attributes and content

Call WriteStartElement, then the attributes and namespace declarations, then the content, then WriteEndElement. The start tag closes when you write the first content, and attributes aren’t accepted after that.

Leaf element

WriteElementString, WriteElementNumber, WriteElementBoolean, and WriteElementBase64String write a start tag, a value, and an end tag in one call. WriteEmptyElement writes an element with no content, <e/> unless XmlWriterOptions.EmptyElementStyle says otherwise, and WriteNilElement writes an empty element with xsi:nil="true".

Attribute with a typed value

WriteAttributeString takes text and WriteAttributeNumber takes an int, a long, or a double. For any other type, open the attribute with WriteStartAttribute, call a value method such as WriteBooleanValue or WriteStringValue(DateTimeOffset), and close it with WriteEndAttribute.

One writer, many documents

Create the writer once, and after each document call Flush and then Reset. Reset discards anything that isn’t flushed. Use Reset(IBufferWriter<byte>) or Reset(Stream) to send the next document somewhere else.

Stream with asynchronous flushes

Write with the synchronous methods, which only fill the writer’s buffer, and call await writer.FlushAsync() at points where a batch is complete. Dispose with await using.

Copy a parsed document

XmlDocument.WriteTo and XmlElement.WriteTo write a parsed document, or one element of it, into a writer. See How to use XmlDocument.

Custom converter

XmlConverter<T>.Write receives the writer, positioned where the value goes. See Custom converters.

Considerations

Know the following before you build on the writer.

Output and buffering

The output is always UTF-8. With an IBufferWriter<byte>, the writer writes into memory it gets from the buffer writer and commits it on Flush, on Dispose, and when it needs more room. With a Stream, it fills a buffer rented from the array pool, which starts at 16 KB, and writes to the stream on Flush, on Dispose, and when the buffer fills. Disposing the writer doesn’t close the stream.

Dispose and open elements

Dispose and DisposeAsync flush. If an element is still open and SkipValidation is false, they flush and then throw an XmlException. In a using statement, that exception replaces an exception that was already on its way out, so the message you catch can be The document has unclosed elements. when the real cause came earlier.

Thread safety

A writer isn’t thread safe. Use each instance from one thread at a time. See Thread safety.

Allocation

A writer that’s reused with Reset allocates nothing per document. Reset discards bytes that are still pending, so call Flush first to keep them. See Benchmarks for the measurements.

Namespaces

The writer tracks the namespaces in scope and declares what’s missing: a default namespace for an element written with a namespace and no prefix, your prefix for an element written with one, and a generated prefix (ns1, ns2, and so on) for an attribute in a namespace that has no prefix in scope. An element written with no namespace argument, or with a null one, gets no declaration, so under a default namespace a reader puts it in that namespace. An element written with an empty namespace is in no namespace: under a default namespace the writer adds xmlns="".

Indentation

With Indented set, elements, comments, and processing instructions each get a line break and indentation. An element that holds text, a CDATA section, or raw XML from WriteRawValue is written on one line, with everything in it.

Layout

Every line break the writer writes is the NewLine of its options, including a line feed inside text, a CDATA section, a comment, or a processing instruction. An element with no content is written in the form EmptyElementStyle asks for (<e/> by default), except after WriteFullEndElement, which always writes an end tag. AttributeStyle decides whether attributes stay on the line of the start tag.

What isn’t checked

  • WriteRawValue copies its input without escaping it. It checks that the input is well formed where it is written only when skipInputValidation is false and the writer does not have SkipValidation set.
  • With SkipValidation set, names, namespace declarations, duplicate attributes, the characters of comments, CDATA sections, and processing instructions, and unclosed elements at Dispose aren’t checked. Values are still escaped, and the single root, MaxDepth, and MaxAttributeCount are still enforced.

Differences from Utf8JsonWriter

  • There’s no property-name step. Structure comes from WriteStartElement and WriteEndElement, and attributes are written on the open start tag.
  • Validation is on by default and also covers names and namespaces. SkipValidation doesn’t turn off the single-root check, MaxDepth, or MaxAttributeCount.
  • Dispose throws when the document is incomplete.

Differences from System.Xml.XmlWriter

  • The output is UTF-8 only, to an IBufferWriter<byte> or a Stream. There’s no TextWriter or StringBuilder target.
  • There’s no WriteDocType, WriteEntityRef, WriteStartDocument, or WriteEndDocument. The declaration is written for you, and nothing closes open elements for you.
  • Reset lets one instance write many documents.
  • Typed values have their own methods, in place of WriteValue overloads and XmlConvert.

Constructors

.ctor

Initializes a new instance of the Utf8XmlWriter class using the specified IBufferWriter`1 to write the output to and customization options.

Syntax

Parameters

Exceptions

.ctor

Initializes a new instance of the Utf8XmlWriter class using the specified stream to write the output to and customization options.

Syntax

Parameters

Exceptions

.ctor Inherited

Inherited from object

Syntax

Properties

BytesCommitted

Gets the total number of bytes committed to the output by the current instance so far.

Syntax

Property Value

Type: long The number of bytes committed to the output. Utf8XmlWriter.Reset sets the value back to 0.

BytesPending

Gets the number of bytes written by the Utf8XmlWriter so far that have not yet been flushed to the output and committed.

Syntax

Property Value

Type: int The number of bytes written so far that have not yet been flushed to the output and committed.

CurrentDepth

Gets the depth of the current element.

Syntax

Property Value

Type: int The number of elements that are open. The value is 0 at the document level, and 1 from the start tag of the root element to its end tag.

Options

Gets the custom behavior when writing XML using this instance, which indicates whether to format the output while writing, whether to skip validation, and which limits apply.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlWriterOptions The options that were passed to the constructor of this instance.

Methods

Dispose

Syntax

DisposeAsync

Syntax

Returns

Type: System.Threading.Tasks.ValueTask

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

Flush

Commits the XML text written so far, which makes it visible to the output destination.

Syntax

Exceptions

Remarks

In the case of IBufferWriter1](https://learn.microsoft.com/dotnet/api/system.buffers.ibufferwriter-1), this advances the underlying [IBufferWriter1 based on what has been written so far. In the case of Stream, this writes the data to the stream and flushes it.

FlushAsync

Asynchronously commits the XML text written so far, which makes it visible to the output destination.

Syntax

Parameters

Returns

Type: System.Threading.Tasks.Task A task representing the asynchronous flush operation.

Exceptions

Remarks

In the case of IBufferWriter1](https://learn.microsoft.com/dotnet/api/system.buffers.ibufferwriter-1), this advances the underlying [IBufferWriter1 based on what has been written so far. In the case of Stream, this writes the data to the stream and flushes it asynchronously, while monitoring cancellation requests.

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

Reset

Resets the internal state of this instance so that it can be reused to write a new document.

Syntax

Exceptions

Remarks

The writer continues to use the original writer options and the original output (either [IBufferWriter1](https://learn.microsoft.com/dotnet/api/system.buffers.ibufferwriter-1) or [Stream](https://learn.microsoft.com/dotnet/api/system.io.stream)). Bytes that are still pending are discarded, as Utf8JsonWriter.Reset` discards them, and Utf8XmlWriter.BytesCommitted is set to 0. Call Utf8XmlWriter.Flush first to keep what has been written.

Reset

Resets the internal state of this instance so that it can be reused with a new instance of IBufferWriter`1.

Syntax

Parameters

Exceptions

Remarks

The writer continues to use the original writer options but now writes to bufferWriter as the new output. Bytes that are still pending are discarded; call Utf8XmlWriter.Flush first to commit them to the previous output.

Reset

Resets the internal state of this instance so that it can be reused with a new instance of Stream.

Syntax

Parameters

Exceptions

Remarks

The writer continues to use the original writer options but now writes to utf8Xml as the new output. Bytes that are still pending are discarded; call Utf8XmlWriter.Flush first to commit them to the previous output.

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?

WriteAttributeNumber

Writes an attribute with the name specified as a string and an Int32 value.

Syntax

Parameters

Exceptions

WriteAttributeNumber

Writes an attribute with the name specified as a string and a Int64 value.

Syntax

Parameters

Exceptions

WriteAttributeNumber

Writes an attribute with the name specified as a string and a Double value.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteAttributeString

Writes an attribute with the name and the value specified as strings.

Syntax

Parameters

Exceptions

WriteAttributeString

Writes an attribute with the local name, the namespace URI, and the value specified as strings.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartAttribute.

WriteAttributeString

Writes an attribute with the prefix, the local name, the namespace URI, and the value specified as strings.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartAttribute.

WriteAttributeString

Writes an attribute with the name and the value specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

WriteAttributeString

Writes an attribute with the name and the value specified as read-only spans of characters.

Syntax

Parameters

Exceptions

WriteBase64StringSegment

Writes a segment of binary data as base64 encoded text to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Examples

Remarks

The segments together are written as Utf8XmlWriter.WriteBase64StringValue writes the whole data: one base64 value, padded only at its end. A segment can be any length. The writer keeps the one or two bytes that do not fill a group of three and encodes them with the next segment. After a segment that is not final, the next call on the writer must be the next segment, apart from Utf8XmlWriter.Flush and Utf8XmlWriter.FlushAsync; any other call throws an XmlException.

WriteBase64StringValue

Writes binary data as base64 encoded text to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteBooleanValue

Writes a Boolean value (as the XML literal true or false) to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteCData

Writes a CDATA section (<![CDATA[...]]>) with the text specified as a string.

Syntax

Parameters

Exceptions

WriteCData

Writes a CDATA section (<![CDATA[...]]>) with the text specified as a read-only span of characters.

Syntax

Parameters

Exceptions

WriteCData

Writes a CDATA section (<![CDATA[...]]>) with the text specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

WriteComment

Writes a comment (<!--...-->) with the text specified as a string.

Syntax

Parameters

Exceptions

Remarks

A comment can be written inside an element, and before or after the root element.

WriteComment

Writes a comment (<!--...-->) with the text specified as a read-only span of characters.

Syntax

Parameters

Exceptions

Remarks

A comment can be written inside an element, and before or after the root element.

WriteComment

Writes a comment (<!--...-->) with the text specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

A comment can be written inside an element, and before or after the root element.

WriteDefaultNamespace

Writes a default namespace declaration (xmlns="namespaceUri") on the start tag of the current element, with the namespace URI specified as a string.

Syntax

Parameters

Exceptions

Remarks

The binding is in scope for the element and its descendants. A declaration counts as an attribute toward XmlWriterOptions.MaxAttributeCount.

WriteDefaultNamespace

Writes a default namespace declaration (xmlns="namespaceUri") on the start tag of the current element, with the namespace URI specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The binding is in scope for the element and its descendants. A declaration counts as an attribute toward XmlWriterOptions.MaxAttributeCount.

WriteElementBase64String

Writes an element with the local name specified as a string and binary data, encoded as base64 text, as its content.

Syntax

Parameters

Exceptions

WriteElementBase64String

Writes an element with the local name specified as a read-only span of characters and binary data, encoded as base64 text, as its content.

Syntax

Parameters

Exceptions

WriteElementBase64String

Writes an element with the local name specified as a read-only span of UTF-8 bytes and binary data, encoded as base64 text, as its content.

Syntax

Parameters

Exceptions

WriteElementBase64String

Writes an element with the local name specified as a pre-encoded XmlEncodedText and binary data, encoded as base64 text, as its content.

Syntax

Parameters

Exceptions

WriteElementBoolean

Writes an element with the local name specified as a string and a Boolean value as its content.

Syntax

Parameters

Exceptions

WriteElementBoolean

Writes an element with the local name specified as a read-only span of characters and a Boolean value as its content.

Syntax

Parameters

Exceptions

WriteElementBoolean

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Boolean value as its content.

Syntax

Parameters

Exceptions

WriteElementBoolean

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Boolean value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a string and an Int32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a string and a Int64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a string and a Double value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a string and a Single value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a string and a Decimal value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a string and a UInt32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a string and a UInt64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and an Int32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a Int64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a Double value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a Single value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a Decimal value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a UInt32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of characters and a UInt64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and an Int32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Int64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Double value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Single value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Decimal value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a UInt32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a UInt64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and an Int32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Int64 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Double value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Single value as its content.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Decimal value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a UInt32 value as its content.

Syntax

Parameters

Exceptions

WriteElementNumber

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a UInt64 value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a string and a string value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name and the namespace URI specified as strings and a string value as its content.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteElementString

Writes an element with the local name specified as a string and a DateTime value as its content.

Syntax

Parameters

Exceptions

Remarks

The value is formatted as it is by Utf8XmlWriter.WriteStringValue.

WriteElementString

Writes an element with the local name specified as a string and a DateTimeOffset value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a string and a Guid value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name and the text content specified as read-only spans of characters.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name and the text content specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a string and text specified as a read-only span of characters as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a string and text specified as a read-only span of UTF-8 bytes as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of characters and a string value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of characters and text specified as a read-only span of UTF-8 bytes as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of characters and a DateTime value as its content.

Syntax

Parameters

Exceptions

Remarks

The value is formatted as it is by Utf8XmlWriter.WriteStringValue.

WriteElementString

Writes an element with the local name specified as a read-only span of characters and a DateTimeOffset value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of characters and a Guid value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a string value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of UTF-8 bytes and text specified as a read-only span of characters as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a DateTime value as its content.

Syntax

Parameters

Exceptions

Remarks

The value is formatted as it is by Utf8XmlWriter.WriteStringValue.

WriteElementString

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a DateTimeOffset value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a read-only span of UTF-8 bytes and a Guid value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a string value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and text specified as a read-only span of characters as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and text specified as a read-only span of UTF-8 bytes as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a DateTime value as its content.

Syntax

Parameters

Exceptions

Remarks

The value is formatted as it is by Utf8XmlWriter.WriteStringValue.

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a DateTimeOffset value as its content.

Syntax

Parameters

Exceptions

WriteElementString

Writes an element with the local name specified as a pre-encoded XmlEncodedText and a Guid value as its content.

Syntax

Parameters

Exceptions

WriteEmptyElement

Writes an empty element (<localName/>) with the local name specified as a string.

Syntax

Parameters

Exceptions

WriteEmptyElement

Writes an empty element with the local name and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteEmptyElement

Writes an empty element with the prefix, the local name, and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteEmptyElement

Writes an empty element (<localName/>) with the local name specified as a read-only span of characters.

Syntax

Parameters

Exceptions

WriteEmptyElement

Writes an empty element with the local name and the namespace URI specified as read-only spans of characters.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteEmptyElement

Writes an empty element with the local name specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

WriteEmptyElement

Writes an empty element with the local name and the namespace URI specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteEmptyElement

Writes an empty element with the prefix, the local name, and the namespace URI specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteEmptyElement

Writes an empty element with the local name specified as a pre-encoded XmlEncodedText.

Syntax

Parameters

Exceptions

WriteEndAttribute

Writes the end of the current attribute.

Syntax

Exceptions

WriteEndElement

Writes the end of the current element.

Syntax

Exceptions

Remarks

An element that has no content is closed as an empty element (/>). Any other element gets an end tag (</name>). An attribute that is still open is ended first.

WriteFullEndElement

Writes the end of the current element as a full end tag (</name>), even when the element has no content.

Syntax

Exceptions

Remarks

An attribute that is still open is ended first.

WriteNamespaceDeclaration

Writes a namespace declaration (xmlns:prefix="namespaceUri") on the start tag of the current element, with the prefix and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

The binding is in scope for the element and its descendants. A declaration counts as an attribute toward XmlWriterOptions.MaxAttributeCount.

WriteNamespaceDeclaration

Writes a namespace declaration (xmlns:prefix="namespaceUri") on the start tag of the current element, with the prefix and the namespace URI specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The binding is in scope for the element and its descendants. A declaration counts as an attribute toward XmlWriterOptions.MaxAttributeCount.

WriteNilElement

Writes an empty element that has the attribute xsi:nil="true", with the local name specified as a string.

Syntax

Parameters

Exceptions

Remarks

If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer declares it on the element with the prefix xsi.

WriteNilElement

Writes an empty element that has the attribute xsi:nil="true", with the local name and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement. If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer declares it on the element with the prefix xsi.

WriteNilElement

Writes an empty element that has the attribute xsi:nil="true", with the local name specified as a read-only span of characters.

Syntax

Parameters

Exceptions

Remarks

If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer declares it on the element with the prefix xsi.

WriteNilElement

Writes an empty element that has the attribute xsi:nil="true", with the local name specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer declares it on the element with the prefix xsi.

WriteNilElement

Writes an empty element that has the attribute xsi:nil="true", with the local name specified as a pre-encoded XmlEncodedText.

Syntax

Parameters

Exceptions

Remarks

If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer declares it on the element with the prefix xsi.

WriteNilValue

Writes the attribute xsi:nil="true" on the start tag of the current element.

Syntax

Exceptions

Remarks

If the XML Schema instance namespace (XmlNamespaces.Xsi) is not in scope, the writer first declares it on the element with the prefix xsi.

WriteNumberValue

Writes an Int32 value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteNumberValue

Writes a Int64 value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteNumberValue

Writes a Double value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteNumberValue

Writes a Single value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

Not-a-number is written as NaN, positive infinity as INF, and negative infinity as -INF.

WriteNumberValue

Writes a Decimal value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteNumberValue

Writes a UInt32 value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteNumberValue

Writes a UInt64 value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteProcessingInstruction

Writes a processing instruction (<?target data?>) with the target and the data specified as strings.

Syntax

Parameters

Exceptions

Remarks

A processing instruction can be written inside an element, and before or after the root element.

WriteRawValue

Writes the input as XML content, with the input specified as a read-only span of UTF-8 bytes. The input is copied to the output without escaping.

Syntax

Parameters

Exceptions

Remarks

Unless skipInputValidation is true or XmlWriterOptions.SkipValidation is set, the writer reads the input before it copies it and throws if the document would not be well formed: inside an element the input must be element content that uses only prefixes in scope, in an attribute it must be an attribute value with no quotation mark, and at the document level it must be comments, processing instructions, white space, and at most the one root element. Inside an element, the input counts as text of that element, so the element’s end tag is not indented. At the document level, the writer does not write the XML declaration before the input.

WriteRawValue

Writes the input as XML content, with the input specified as a read-only span of characters. The input is copied to the output without escaping.

Syntax

Parameters

Exceptions

Remarks

Unless skipInputValidation is true or XmlWriterOptions.SkipValidation is set, the writer reads the input before it copies it and throws if the document would not be well formed: inside an element the input must be element content that uses only prefixes in scope, in an attribute it must be an attribute value with no quotation mark, and at the document level it must be comments, processing instructions, white space, and at most the one root element. Inside an element, the input counts as text of that element, so the element’s end tag is not indented. At the document level, the writer does not write the XML declaration before the input.

WriteRawValue

Writes the input as XML content, with the input specified as a string. The input is copied to the output without escaping.

Syntax

Parameters

Exceptions

Remarks

Unless skipInputValidation is true or XmlWriterOptions.SkipValidation is set, the writer reads the input before it copies it and throws if the document would not be well formed: inside an element the input must be element content that uses only prefixes in scope, in an attribute it must be an attribute value with no quotation mark, and at the document level it must be comments, processing instructions, white space, and at most the one root element. Inside an element, the input counts as text of that element, so the element’s end tag is not indented. At the document level, the writer does not write the XML declaration before the input.

WriteStartAttribute

Writes the beginning of an attribute with the name specified as a string.

Syntax

Parameters

Exceptions

Remarks

Write the value with one or more calls to the value methods, such as Utf8XmlWriter.WriteStringValue, and then call Utf8XmlWriter.WriteEndAttribute.

WriteStartAttribute

Writes the beginning of an attribute with the name specified as a read-only span of characters.

Syntax

Parameters

Exceptions

Remarks

Write the value with one or more calls to the value methods, such as Utf8XmlWriter.WriteStringValue, and then call Utf8XmlWriter.WriteEndAttribute.

WriteStartAttribute

Writes the beginning of an attribute with the prefix, the local name, and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

With a prefix, the writer declares prefix on the element unless it is already bound to namespaceUri. With a namespace URI and no prefix, the writer uses a prefix that is already bound to namespaceUri, or declares a generated prefix (ns1, ns2, and so on) on the element. Write the value with one or more calls to the value methods, such as Utf8XmlWriter.WriteStringValue, and then call Utf8XmlWriter.WriteEndAttribute.

WriteStartElement

Writes the start tag of an element with the local name specified as a string.

Syntax

Parameters

Exceptions

Remarks

The start tag stays open for attributes and namespace declarations until content or an end tag is written.

WriteStartElement

Writes the start tag of an element with the local name and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

If namespaceUri is the default namespace in scope, the element is written without a prefix. If it is bound to a prefix that is in scope, the element is written with that prefix. Otherwise, the writer declares it as the default namespace on the element. For an empty string, that declaration is xmlns="", and it is written only when a default namespace is in scope.

WriteStartElement

Writes the start tag of an element with the prefix, the local name, and the namespace URI specified as strings.

Syntax

Parameters

Exceptions

Remarks

The element is written with prefix. Unless prefix is already bound to namespaceUri, the writer declares it on the element, even when the namespace has another prefix in scope. Without a prefix, this overload behaves as Utf8XmlWriter.WriteStartElement does.

WriteStartElement

Writes the start tag of an element with the local name specified as a read-only span of characters.

Syntax

Parameters

Exceptions

WriteStartElement

Writes the start tag of an element with the local name specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Exceptions

WriteStartElement

Writes the start tag of an element with the local name specified as a pre-encoded XmlEncodedText.

Syntax

Parameters

Exceptions

WriteStartElement

Writes the start tag of an element with the local name and the namespace URI specified as read-only spans of characters.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteStartElement

Writes the start tag of an element with the local name and the namespace URI specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteStartElement

Writes the start tag of an element with the prefix, the local name, and the namespace URI specified as read-only spans of UTF-8 bytes.

Syntax

Parameters

Exceptions

Remarks

The namespace is handled as it is by Utf8XmlWriter.WriteStartElement.

WriteStringValue

Writes text specified as a string to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

The text is escaped as needed: & and < become entity references, and a character that would not survive a round trip (such as a carriage return, or a tab or line feed in an attribute value) becomes a character reference.

WriteStringValue

Writes text specified as a read-only span of characters to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

The text is escaped as it is by Utf8XmlWriter.WriteStringValue.

WriteStringValue

Writes text specified as a read-only span of UTF-8 bytes to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

The text is escaped as it is by Utf8XmlWriter.WriteStringValue.

WriteStringValue

Writes a DateTime value as an XML Schema dateTime to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

A value whose Kind is Utc ends with Z. A Local value ends with the offset of the local time zone. A Unspecified value has no time zone.

WriteStringValue

Writes a DateTimeOffset value as an XML Schema dateTime with its offset to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteStringValue

Writes a DateOnly value as an XML Schema date to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteStringValue

Writes a TimeOnly value as an XML Schema time to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteStringValue

Writes a TimeSpan value as an XML Schema duration to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteStringValue

Writes a Guid value to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

WriteStringValueSegment

Writes a segment of text specified as a read-only span of UTF-8 bytes to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Examples

Remarks

The segments together are written as Utf8XmlWriter.WriteStringValue writes the whole text. A segment can end in the middle of a multi-byte character: the writer keeps the bytes of the incomplete character and writes it when the next segment completes it. After a segment that is not final, the next call on the writer must be the next segment, apart from Utf8XmlWriter.Flush and Utf8XmlWriter.FlushAsync; any other call throws an XmlException.

WriteStringValueSegment

Writes a segment of text specified as a read-only span of characters to the content of the current element or to the value of the current attribute.

Syntax

Parameters

Exceptions

Remarks

The segments together are written as Utf8XmlWriter.WriteStringValue writes the whole text. A segment can end between the two halves of a surrogate pair: the writer keeps the first half and writes the character when the next segment brings the second. After a segment that is not final, the next call on the writer must be the next segment, apart from Utf8XmlWriter.Flush and Utf8XmlWriter.FlushAsync; any other call throws an XmlException.

WriteXmlDeclaration

Writes the XML declaration (<?xml version="1.0" encoding="utf-8"?>).

Syntax

Exceptions

Remarks

The declaration carries the version from XmlWriterOptions.Version, the encoding utf-8, and the standalone pseudo-attribute when XmlWriterOptions.Standalone is set. A byte order mark comes first when XmlWriterOptions.EmitByteOrderMark is true. The writer writes the declaration by itself before the first element, comment, or processing instruction unless XmlWriterOptions.OmitXmlDeclaration is true. Call this method to write the declaration even when that option is true.
  • CloudNimble.Text.Xml.XmlWriterOptions
  • CloudNimble.Text.Xml.XmlEncodedText
  • CloudNimble.Text.Xml.XmlVersion
  • CloudNimble.Text.Xml.XmlException
  • CloudNimble.Text.Xml.XmlNamespaces
  • CloudNimble.Text.Xml.Utf8XmlReader
  • CloudNimble.Text.Xml.XmlDocument
  • CloudNimble.Text.Xml.Serialization.XmlConverter