Export (0) Print
Expand All

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

  1. Create an instance of the XsdDataContractExporter.

  2. 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.

  3. 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.

  4. Call one of the Export methods. There are three overloads taking a Type, a List of Type objects, or a List 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 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.

    Aa702692.note(en-us,VS.90).gifNote:
    If there is a failure during export, the XmlSchemaSet will be in an unpredictable state.

  5. 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:

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

See Also

Community Additions

ADD
Show:
© 2014 Microsoft