CrystalXmlNamespace

Namespace: SnowBank.Data.Xml · struct

Implements: IEquatable<CrystalXmlNamespace>

Namespace URI of an XML element or attribute, held in both its text and UTF-8 representations

Remarks

Same dual representation, and for the same reason, as CrystalXmlName: a namespace URI reaches the output as the value of a declaration attribute, so the char core copies Text and the byte core copies Utf8, and neither ever transcodes.

A namespace carries no prefix. The prefix an element or attribute is written under depends on the depth of the element that declares it and on what is already in scope, so it belongs to the emitter and is assigned there; the same namespace value is reused across documents and depths without change.

Three states, and they are distinct in the output:

  • None (the default value): no namespace was specified. An element in this state inherits whatever default namespace is in scope, and an attribute in this state is in no namespace at all.
  • The empty URI, from Create(""): the absence of a namespace, stated explicitly. This is what [DataContract(Namespace = "")] means, and declaring it writes xmlns="".
  • An absolute URI: the namespace itself.

Constructors

CrystalXmlNamespace

CrystalXmlNamespace(string uri, ReadOnlyMemory<byte> utf8)

Constructs a namespace from its two representations

  • uri — Text representation of the URI, as written by the char core
  • utf8 — UTF-8 representation of uri, as written by the byte core

The caller is responsible for the two representations agreeing, and for being a legal namespace URI: this is the trusted, non-validating path, for the frozen literal pairs generated code emits. Prefer for anything else.

Properties

IsEmpty

bool IsEmpty { get; }

Whether this is the empty URI, the explicitly stated absence of a namespace

IsNone

bool IsNone { get; }

Whether no namespace was specified at all

Distinct from : see the type remarks.

None

static CrystalXmlNamespace None { get; }

The absence of a specified namespace: an element inherits the default namespace in scope, an attribute is in no namespace

Text

string Text { get; }

Text representation of the URI, or null when no namespace was specified

Utf8

ReadOnlySpan<byte> Utf8 { get; }

UTF-8 representation of the URI

Methods

Create

static CrystalXmlNamespace Create(string uri)

Builds a namespace from its URI, validating it and computing the UTF-8 representation now

  • uri — Namespace URI, or the empty string for the explicit absence of a namespace

Returns: Namespace usable by both output cores

This transcodes, validates and allocates, so it belongs at setup time, never on a per-element path. The URI must be ABSOLUTE, which is what XML namespaces require and what every namespace the DataContract format uses is (http://schemas.datacontract.org/2004/07/Acme, urn:acme:catalog:1); a relative reference is rejected here rather than written into a document no reader can resolve.

The four characters an attribute value has to escape are rejected too, so that an emitter writes a URI the same way it writes a name: one copy of whichever representation matches its output unit, with no scan and no escaper. A URI that needs one of them percent-encodes it, which is what the URI rules ask for anyway.

Equals

bool Equals(CrystalXmlNamespace other)

bool Equals(object obj)

GetHashCode

int GetHashCode()

ToString

string ToString()