AdditionalFiles item, and the XmlEssentials source generator writes a class for each element and complex type, an enumeration for each enumerated simple type, and an XmlSerializerContext that reads and writes them. This article shows the setup and lists what the generator maps and what it doesn’t.
The schema is a build-time input. It isn’t loaded, embedded, or checked at run time, so the generated code works in a Native AOT application. XmlEssentials doesn’t validate documents against a schema.
Generate the types
1
Add the schema file to the project
Put the
.xsd file in your project, for example in a Schemas folder. This article uses the following schema:Schemas/catalog.xsd
2
Declare it as an AdditionalFiles item
In the project file, add the schema as an
AdditionalFiles item. Optionally, set the namespace of the generated code and the name of the generated context:MyApp.csproj
3
Build the project
Build. The generator adds the classes and the context to the compilation. There are no files to check in.
4
Use the generated classes and context
In your code, create the generated classes as you would your own, and pass the context’s type info to The code produces the following output:
XmlSerializer:Program.cs
Weight as nillable, the generated property is double?, and a null value is written with xsi:nil="true".
View the complete sample on GitHub
What the generator writes
For the schema above, the generator writes two classes for theCatalog element, one class for the Supplier type, one enumeration, and one context. Three of them are shown here, without their file headers and namespace:
Generated code
- Names come from the schema unchanged. The attributes
publishedandskubecome properties with those exact names, lowercase included. A character that isn’t valid in a C# identifier becomes an underscore. - An anonymous complex type is named after its parent. The
Productelement insideCatalogbecomesCatalog_Product. - An attribute is optional unless the schema says
use="required".publishedis aDateOnly?, so that it can be absent. - Only
nillable="true"allowsxsi:nil.Weightis nillable, so anullis written asxsi:nil="true".NameandSupplieraren’t, so anullis left out: that is what[XmlIgnore(Condition = XmlIgnoreCondition.WhenWritingNull)]says. - Every class is
public partial. Add members in a file of your own instead of editing generated code. xs:documentationbecomes a<summary>comment on the class. Documentation on child elements and attributes isn’t copied.
public partial class in the same namespace, with one XmlTypeInfo<T> property for each generated class and enumeration: Catalog, Catalog_Product, Supplier, and Availability. It has the members every generated context has. See What the generator adds to your class.
Item metadata
The generator sees this metadata because the package’s build file makes it visible to the compiler. Every file whose name ends in
.xsd is processed. Other AdditionalFiles items, including .wsdl files, are ignored.
The target namespace
TheDefault instance of a generated context has DefaultNamespace set to the schema’s targetNamespace, so documents are written in that namespace with no further setup:
Program.cs
How schema constructs are mapped
Types
Types such as
xs:positiveInteger, xs:hexBinary, and xs:token map to string, and so does a simple type that is a list or a union. Facets such as pattern and minInclusive aren’t checked.Structure
A document names a derived type the way the schema says: with
xsi:type. The value is a qualified name, so xsi:type="s:Circle" and xsi:type="Circle" are both read as the Circle class, whatever prefix the document chose.
Limitations
The generator maps a schema to classes. It isn’t a validator, and it doesn’t cover every construct. The table lists what it doesn’t act on. It reportsXMLESS011 as information when the documents stay the same, and XMLESS018 as a warning when a document the schema allows can’t be read or written as the schema describes it.
Diagnostics
Options and the generated context
A context generated from a schema is a default-mode context: it has metadata for each class, and a write fast path for each class that can have one. A class that may hold a derived class, an element choice, mixed content, extension data, or an element in no namespace is written from its metadata. The context applies everyXmlSerializerOptions setting and custom converter, as a context you declare yourself does. See How a context applies your options.
The generated context is used when you pass its type info, or the context, to XmlSerializer. A call that passes only a value, or a value and options, maps the generated classes through reflection, as it does any other class. Interceptors redirect calls only for contexts declared in your source code.
See also
Use source generation
Contexts for classes you write yourself.
Publish with Native AOT
Schema-generated types in an AOT application.
Use XML namespaces
How the serializer writes and matches namespaces.
Null and nil
How
xsi:nil is written and read.