PhoenixmlDb.Core
XdmValue
Represents an XDM atomic value — a typed, immutable value from the XPath/XQuery type system.
#XdmValue
Namespace: PhoenixmlDb.Xdm
Represents an XDM atomic value — a typed, immutable value from the XPath/XQuery type system.
XdmValue pairs a CLR value (boxed in XdmValue.RawValue) with an XdmType tag that identifies its XSD type. This enables type-aware operations such as comparisons, arithmetic, and casting as defined by the XPath/XQuery Functions and Operators specification.
Construction: Use the static factory methods (XdmValue.UntypedAtomic, XdmValue.XsString, XdmValue.XsInteger, XdmValue.Boolean, etc.) to create values. These ensure the correct XdmType tag is applied.
Access: Use the typed accessor methods (XdmValue.AsString, XdmValue.AsLong, XdmValue.AsDouble, XdmValue.AsBoolean, etc.) to extract the value. These perform type coercion following XPath casting rules and throw InvalidCastException for incompatible types.
Equality: Two values are equal if and only if they have the same XdmValue.Type and the same underlying value.
#Example
var price = XdmValue.XsDecimal(19.99m);
var name = XdmValue.XsString("Widget");
var active = XdmValue.Boolean(true);
Console.WriteLine(price.AsDecimal()); // 19.99
Console.WriteLine(name.AsString()); // Widget
Console.WriteLine(active.AsBoolean()); // True
#Properties
| Name | Description |
|---|---|
RawValue
|
Gets the raw value object. |
#Methods
#From``1(``0)
Converts a supported CLR value to its XDM representation. This is the single conversion point from .NET types to XDM values.
Parameters:
-
value— The value to convert. Dispatch is on the value's runtime type, soFrom<object>("x")andFrom<string>("x")both produceXdmValue.XsString— a metadata value's XDM type is a property of what it actually holds, not of how it happens to be typed at the call site. Anullreference or unsetNullable<T>producesXdmValue.Empty.
Exceptions:
-
NotSupportedException— The CLR type has no XDM equivalent.
PhoenixmlDb.Linq
delegates to this instead of maintaining its own parallel
Type
-to-XDM map, so the two projects cannot drift apart on how a given CLR type is represented.
#IsSupportedClrType(Type)
Determines whether
and
support the given CLR type.
Nullable<T>
wrappers are supported wherever their underlying type is.
and
are always reported as supported:
dispatches on the value's runtime type rather than the compile-time type argument, so a caller guarding a call with
IsSupportedClrType(typeof(T))
before invoking
From<T>
must not be turned away just because
T
is
— whether the call actually succeeds still depends on what the value holds at run time.
#To``1(PhoenixmlDb.Xdm.XdmValue)
Converts this value to a supported CLR type. The stored
must match
's XDM family — this is a strict, storage-boundary read, not the coercive casting the
As*
accessors perform for XPath expression evaluation. Numeric widening/narrowing within a family is still permitted (an
xs:integer
reads back as
,
,
, or
; an
xs:double
/
xs:float
reads back as either
or
).
Type parameters:
-
T— The requested CLR type.Nullable<T>wrappers are supported wherever their underlying type is; requestingXdmValueitself returns the value unchanged; requestingObjectreturns whatever CLR value the storedXdmTypenaturally boxes to (the mirror image ofXdmValue.From``1dispatching on the runtime value forT = object), ornullforXdmValue.Empty.
Exceptions:
-
NotSupportedException— The CLR type has no XDM equivalent. -
InvalidCastException— Either the storedXdmTypeis not oneTcan be read from (e.g. requestingInt32from a value stored asxs:string), or this value isXdmValue.EmptyandTis a non-nullable value type with no meaningful empty representation. -
OverflowException— The stored integer does not fit in the requested narrower CLR type (e.g. requestingBytefor a storedxs:integerof300).
XdmValue.Empty is handled by whether T has a value that can genuinely mean "absent": if T is a reference type or Nullable<V>, converting an empty value returns null — this is what keeps From<string>(null) (which produces XdmValue.Empty) and To<string>() genuine inverses of each other. If T is a non-nullable value type (Int64, DateTimeOffset, etc.), there is no such thing as "the empty Int64" — returning a default like 0 or year 1 would be a silently wrong value indistinguishable from genuine data, so this throws InvalidCastException instead.