EXI
Encode XML information as a compact binary stream and decode it back into the data model.
Packages: DCoding.Xml.Exi
On this page
Binary representation of XML information
Efficient XML Interchange is a W3C format for compact XML information. It is distinct from simply compressing a UTF-8 XML file. A successful round trip concerns the selected information and fidelity options, not necessarily identical XML text.
A schema-less round trip
The example converts an XDocument into materialized XDM content, explicitly selects byte-aligned, schema-less processing for encoder and decoder, and checks the decoded element, value and attribute in XDM. The caller owns the stream. Decoding does not recreate every piece of source metadata; exporting the result to LINQ to XML requires separate checks.
using System.Xml.Linq;
using DCoding.Xml.Xdm;
using DCoding.Xml.Exi;
var document = XDocument.Parse("<reading unit=\"kWh\">12.5</reading>");
var node = XdmXmlAdapter.FromXDocument(document);
var content = new ExiMaterializedContent(new[] { node });
using var binary = new MemoryStream();
var options = new ExiOptions(alignment: ExiAlignment.ByteAligned,
schemaId: ExiSchemaId.SchemaLess);
var encoded = ExiEncoder.Encode(new ExiEncodeRequest(content, binary, options, leaveOpen: true));
binary.Position = 0;
var decoded = ExiDecoder.Decode(new ExiDecodeRequest(binary, options, leaveOpen: true));
var restored = decoded.Content.Nodes.Single().Children.Single();
if (restored.LocalName != "reading" || restored.StringValue != "12.5"
|| restored.Attributes.Single().StringValue != "kWh")
throw new InvalidOperationException("EXI round trip failed.");
Console.WriteLine($"{restored.LocalName}: {restored.StringValue}");
Download example project ZIP · C# · .NET 8 / 10 / 11
Agree the wire format
For interoperability, both ends must agree on options and any schema grammars. Application protocols can impose additional constraints. EXI support alone is not certification for ISO 15118 or another application protocol.
Absent is different from schema-less
ExiSchemaId.SchemaLess explicitly selects schema-less processing. ExiSchemaId.Absent means that the schema choice must be established out of band; it is not another spelling of schema-less. BuiltIn and an identified grammar are separate states. Set the agreed state deliberately when constructing custom ExiOptions, rather than assuming that omitted properties always encode the agreement you intend.
Choose one alignment mode
The four modes describe different wire encodings, not interchangeable decoder guesses. Choose according to the protocol and measure representative messages if size or throughput determines the decision.
| Mode | Encoding shape |
|---|---|
| BitPacked | Event and value coding can cross byte boundaries. |
| ByteAligned | Event/value coding uses byte alignment. |
| PreCompression | Channelized form without the compression step. |
| Compression | Channelized form with compression. |
A practical option checklist
Record the EXI agreement alongside the protocol integration. Do not guess options from an arbitrary binary payload. Schema identifiers identify an agreement; they do not grant permission to fetch a resource.
- Document or fragment scope and alignment mode.
- Schema-less or schema-informed coding, with agreed grammar identity.
- Fidelity requirements, such as comments, prefixes and processing instructions.
- Finite input, value, grammar and output limits.
- Separate requirements for Canonical EXI or an application protocol.
Common questions
Will the original XML bytes be reproduced?
Not in general. XML information, lexical details and source metadata are different things. Test the information your protocol needs under the selected fidelity agreement.
Does schema-informed runtime use imply a compiler licence?
Materialized EXI runtime processing and grammar compilation are separate capabilities. Use the edition matrix to distinguish runtime support, compiler/build tools and streaming.