Skip to main content
Generating types from a schema is a preview feature. It maps the constructs this article lists, and it doesn’t yet cover all of XML Schema: see Limitations. The generated classes and the diagnostics can change between releases. Check the generated code against real documents before you rely on it.
When a partner gives you an XML Schema (XSD) file, you don’t have to write the classes by hand. Add the schema to your project as an 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 XmlSerializer:
Program.cs
The code produces the following output:
The second product has no weight. Because the schema marks 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 the Catalog 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
What to notice:
  • Names come from the schema unchanged. The attributes published and sku become 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 Product element inside Catalog becomes Catalog_Product.
  • An attribute is optional unless the schema says use="required". published is a DateOnly?, so that it can be absent.
  • Only nillable="true" allows xsi:nil. Weight is nillable, so a null is written as xsi:nil="true". Name and Supplier aren’t, so a null is 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:documentation becomes a <summary> comment on the class. Documentation on child elements and attributes isn’t copied.
The context is a 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

The Default 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
The code produces the following output:
View the complete sample on GitHub
When you create the context from your own options, as the first example does, set DefaultNamespace yourself. Constructor options replace the generated defaults, and without the namespace the document element is written unqualified, which the schema doesn’t allow.
The members of the generated classes have no namespace of their own, so when reading, the serializer matches child elements and attributes by local name. See Know what the serializer checks.

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 reports XMLESS011 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.
One limitation loses information: a repeating group that holds xs:any, or one element name with two types, becomes one list for each element, so the order between elements of different lists isn’t kept when the document is written back. Treat XMLESS018 as something to check before you rely on the generated classes for that part of the schema.

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 every XmlSerializerOptions 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.