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.
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. Call one of the CanExport methods. The method determines whether the specified type can be exported. The method has the same overloads as the
Exportmethod in the next step.
Call one of the Export methods. There are three overloads taking a Type, a List<T> of
Typeobjects, or a List<T> of Assembly objects. In the last case, all types in all the given assemblies are exported.
Multiple calls to the
Exportmethod results in multiple items being added to the same
XmlSchemaSet. A type is not generated into the
XmlSchemaSetif it already exists there. Therefore, calling
Exportmultiple times on the same
XsdDataContractExporteris preferable to creating multiple instances of the
XsdDataContractExporterclass. This avoids duplicate schema types from being generated.
If there is a failure during export, the
XmlSchemaSetwill be in an unpredictable state.
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
Typerepresents the known types for the types being exported. (For more information, seeData Contract Known Types.) These known types are exported on every
Exportcall in addition to the types passed to the
DataContractSurrogate. An IDataContractSurrogate can be supplied through this property that will customize the export process. For more information, seeData Contract Surrogates. By default, no surrogate is used.
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
Typeand 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
Typeand 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 returns
GetSchemaType method. This method works only with IXmlSerializable types that are represented as anonymous types in the schema, and returns
nullfor all other types. For anonymous types, this method returns an XmlSchemaType that represents a given
Export options affect all of these methods.