Exporting Schemas from Classes
To generate XML Schema definition language (XSD) schemas from classes that are used in the data contract model, use the XsdDataContractExporter class. This topic describes the process for creating schemas.
The Export Process
The schema export process starts with one or more types and produces an XmlSchemaSet that describes the XML projection of these types.
The XmlSchemaSet
is part of the .NET Frameworkâs Schema Object Model (SOM) that represents a set of XSD Schema documents. To create XSD documents from an XmlSchemaSet
, use the collection of schemas from the Schemas property of the XmlSchemaSet
class. Then serialize each XmlSchema object using the XmlSerializer.
To export schemas
Create an instance of the XsdDataContractExporter.
Optional. Pass an XmlSchemaSet in the constructor. In this case, the schema generated during the schema export is added to this XmlSchemaSet instance instead of starting with a blank XmlSchemaSet.
Optional. Call one of the CanExport methods. The method determines whether the specified type can be exported. The method has the same overloads as the
Export
method in the next step.Call one of the Export methods. There are three overloads taking a Type, a List<T> of
Type
objects, or a List<T> of Assembly objects. In the last case, all types in all the given assemblies are exported.Multiple calls to the
Export
method results in multiple items being added to the sameXmlSchemaSet
. A type is not generated into theXmlSchemaSet
if it already exists there. Therefore, callingExport
multiple times on the sameXsdDataContractExporter
is preferable to creating multiple instances of theXsdDataContractExporter
class. This avoids duplicate schema types from being generated.Note
If there is a failure during export, the
XmlSchemaSet
will be in an unpredictable state.Access the XmlSchemaSet through the Schemas property.
Export Options
You can set the Options property of the XsdDataContractExporter to an instance of the ExportOptions class to control various aspects of the export process. Specifically, you can set the following options:
KnownTypes. This collection of
Type
represents the known types for the types being exported. (For more information, see Data Contract Known Types.) These known types are exported on everyExport
call in addition to the types passed to theExport
method.DataContractSurrogate. An IDataContractSurrogate can be supplied through this property that will customize the export process. For more information, see Data Contract Surrogates. By default, no surrogate is used.
Helper Methods
In addition to its primary role of exporting schema, the XsdDataContractExporter
provides several useful helper methods that provide information about types. These include:
GetRootElementName method. This method takes a
Type
and returns an XmlQualifiedName that represents the root element name and namespace that would be used if this type were serialized as the root object.GetSchemaTypeName method. This method takes a
Type
and returns an XmlQualifiedName that represents the name of the XSD schema type that would be used if this type were exported to the schema. For IXmlSerializable types represented as anonymous types in the schema, this method returnsnull
.GetSchemaType method. This method works only with IXmlSerializable types that are represented as anonymous types in the schema, and returns
null
for all other types. For anonymous types, this method returns an XmlSchemaType that represents a givenType
.
Export options affect all of these methods.