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.
Multiple calls to the Export method results in multiple items being added to the same XmlSchemaSet. A type is not generated into the XmlSchemaSet if it already exists there. Therefore, calling Export multiple times on the same XsdDataContractExporter is preferable to creating multiple instances of the XsdDataContractExporter 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.
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 every Export call in addition to the types passed to the Export 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.
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 returns null.
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 given Type.
Export options affect all of these methods.