Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml Inheritance: System.ValueTypeSyntax
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:
Utf8XmlWriter.WriteStartElement or Utf8XmlWriter.WriteEmptyElement:
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 readonlyfield on the type that writes the element is the usual place. CallingEncodeeach 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
==orEquals. Both compare the bytes. Comparing the results ofToStringcreates two strings to do the same thing. - Reach for a
u8literal first.writer.WriteStartElement("Item"u8)also skips the conversion and needs no field. UseXmlEncodedTextwhen the name isn’t known at compile time, or when you want one named value to share.
Considerations
Encodedoesn’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, unlessXmlWriterOptions.SkipValidationis set. This is unlikeJsonEncodedText, which escapes when it encodes.Encodeallocates 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 theWriteElement*methods. LikeWriteStartElement(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 thestringor UTF-8 overloads. - The default value is an empty name.
default(XmlEncodedText)has an emptyEncodedUtf8Bytes, and the writer rejects an empty name unlessSkipValidationis 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.ValueTypeSyntax
.ctor Inherited
Inherited from
objectSyntax
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.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
Syntax
Returns
Type:int
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
Syntax
Returns
Type:string
ToString Override
Inherited from
System.ValueTypeSyntax
Returns
Type:string?
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.Utf8XmlWriter
- CloudNimble.Text.Xml.XmlWriterOptions
- CloudNimble.Text.Xml.XmlName