Skip to main content

Definition

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

Syntax

Summary

Represents an XML name that is already encoded as UTF-8, so that a Utf8XmlWriter can write it many times without encoding it again.

Usage

XmlEncodedText holds an element name as UTF-8 bytes, so that a Utf8XmlWriter can write the name many times without converting it from a string each time. It’s the counterpart of JsonEncodedText. Create a value once with XmlEncodedText.Encode and keep it in a static field:
Then pass it to Utf8XmlWriter.WriteStartElement or Utf8XmlWriter.WriteEmptyElement:
View the complete sample on GitHub Encode accepts a string, a ReadOnlySpan<char>, or UTF-8 bytes. EncodedUtf8Bytes returns the stored bytes, and ToString returns the name as a string.

Remarks

Create a value once with XmlEncodedText.Encode and reuse it for a name that is written often. Encoding converts the name to UTF-8. It does not escape or check the name. The writer checks the name when it writes it.

Best Practices

  • Encode once and keep the value. A static readonly field on the type that writes the element is the usual place. Calling Encode each time you write gains nothing.
  • Use it for names only. A name is never escaped, so text passed this way isn’t either.
  • Compare values with == or Equals. Both compare the bytes. Comparing the results of ToString creates two strings to do the same thing.
  • Reach for a u8 literal first. writer.WriteStartElement("Item"u8) also skips the conversion and needs no field. Use XmlEncodedText when the name isn’t known at compile time, or when you want one named value to share.

Considerations

  • Encode doesn’t check the name. It only converts to UTF-8. The writer checks that the bytes are a valid XML name each time it writes them, unless XmlWriterOptions.SkipValidation is set. This is unlike JsonEncodedText, which escapes when it encodes.
  • Encode allocates one array and copies its input into it, including the overload that takes UTF-8 bytes.
  • The writer methods that take it write an element with no prefix and no namespace argument: WriteStartElement, WriteEmptyElement, WriteNilElement, and the WriteElement* methods. Like WriteStartElement(string), they declare no namespace for the element, so under a default namespace the element is in that namespace. For attribute names, prefixed names, and names with a namespace argument, use the string or UTF-8 overloads.
  • The default value is an empty name. default(XmlEncodedText) has an empty EncodedUtf8Bytes, and the writer rejects an empty name unless SkipValidation is set.
  • Equality is by bytes. Two values are equal when their UTF-8 bytes are the same, with no Unicode normalization.

Constructors

.ctor Inherited

Inherited from System.ValueType

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

EncodedUtf8Bytes

Gets the UTF-8 encoded representation of the name.

Syntax

Property Value

Type: System.ReadOnlySpan<byte> The UTF-8 bytes of the name. The span is empty for a default XmlEncodedText.

Methods

Encode

Encodes a name specified as a string to UTF-8.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlEncodedText The UTF-8 encoded name.

Exceptions

Encode

Encodes a name specified as a read-only span of characters to UTF-8.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlEncodedText The UTF-8 encoded name.

Encode

Creates an encoded name from a name specified as a read-only span of UTF-8 bytes.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlEncodedText The UTF-8 encoded name.

Equals

Syntax

Parameters

Returns

Type: bool

Equals Override

Syntax

Parameters

Returns

Type: bool

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

Syntax

Returns

Type: int

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

Syntax

Returns

Type: string

ToString Override

Inherited from System.ValueType

Syntax

Returns

Type: string?

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Utf8XmlWriter
  • CloudNimble.Text.Xml.XmlWriterOptions
  • CloudNimble.Text.Xml.XmlName