SchemaImporterExtension.ImportSchemaType Method

Definition

Allows you to manipulate the code generated by examining the imported schema and specifying the CLR type that it maps to.

Overloads

ImportSchemaType(XmlSchemaType, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider)

Allows you to manipulate the code generated by examining the imported schema and specifying the CLR type that it maps to.

ImportSchemaType(String, String, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider)

Allows you to manipulate the code generated by examining the imported schema and specifying the CLR type that it maps to.

Examples

The example below shows a simple implementation of the ImportSchemaType(String, String, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider) method. The code checks the namespace and name. When the right values are found, it adds the name of a custom assembly ("Order.dll") that must be referenced in the generated code. It then adds a new namespace ("Microsoft.Samples") that is generated in the code.

C#
public override string ImportSchemaType(string name, string ns,
    XmlSchemaObject context, XmlSchemas schemas,
    XmlSchemaImporter importer,
    CodeCompileUnit compileUnit, CodeNamespace codeNamespace,
    CodeGenerationOptions options, CodeDomProvider codeGenerator)
    {
        if (name.Equals("Order") && ns.Equals("http://orders/"))
        {
            compileUnit.ReferencedAssemblies.Add("Order.dll");
            codeNamespace.Imports.Add
               (new CodeNamespaceImport("Microsoft.Samples"));
             return "Order";
        }
        return null;
    }

ImportSchemaType(XmlSchemaType, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider)

Allows you to manipulate the code generated by examining the imported schema and specifying the CLR type that it maps to.

C#
public virtual string ImportSchemaType(System.Xml.Schema.XmlSchemaType type, System.Xml.Schema.XmlSchemaObject context, System.Xml.Serialization.XmlSchemas schemas, System.Xml.Serialization.XmlSchemaImporter importer, System.CodeDom.CodeCompileUnit compileUnit, System.CodeDom.CodeNamespace mainNamespace, System.Xml.Serialization.CodeGenerationOptions options, System.CodeDom.Compiler.CodeDomProvider codeProvider);

Parameters

type
XmlSchemaType

The XSD type.

context
XmlSchemaObject

The schema information, such as the line number of the XML element.

schemas
XmlSchemas

The collection of schemas in the document.

importer
XmlSchemaImporter

The importer being used.

compileUnit
CodeCompileUnit

A CodeCompileUnit to which you can add CodeDOM structures to generate alternative code for the XSD.

mainNamespace
CodeNamespace

The current namespace for the element.

options
CodeGenerationOptions

A bitwise combination of the enumeration values that specify the setting options on the code compiler.

codeProvider
CodeDomProvider

A CodeDomProvider that is used to generate the new code.

Returns

The name of the CLR type that this maps to.

Examples

The example below shows a simple implementation of the ImportSchemaType(String, String, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider) method. The code checks the namespace and name. When the right values are found, it adds the name of a custom assembly ("Order.dll") that must be referenced in the generated code. It then adds a new namespace ("Microsoft.Samples") that is generated in the code.

C#
public override string ImportSchemaType(string name, string ns,
    XmlSchemaObject context, XmlSchemas schemas,
    XmlSchemaImporter importer,
    CodeCompileUnit compileUnit, CodeNamespace codeNamespace,
    CodeGenerationOptions options, CodeDomProvider codeGenerator)
    {
        if (name.Equals("Order") && ns.Equals("http://orders/"))
        {
            compileUnit.ReferencedAssemblies.Add("Order.dll");
            codeNamespace.Imports.Add
               (new CodeNamespaceImport("Microsoft.Samples"));
             return "Order";
        }
        return null;
    }

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ImportSchemaType(String, String, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider)

Allows you to manipulate the code generated by examining the imported schema and specifying the CLR type that it maps to.

C#
public virtual string ImportSchemaType(string name, string ns, System.Xml.Schema.XmlSchemaObject context, System.Xml.Serialization.XmlSchemas schemas, System.Xml.Serialization.XmlSchemaImporter importer, System.CodeDom.CodeCompileUnit compileUnit, System.CodeDom.CodeNamespace mainNamespace, System.Xml.Serialization.CodeGenerationOptions options, System.CodeDom.Compiler.CodeDomProvider codeProvider);

Parameters

name
String

The name of the element.

ns
String

The namespace of the element.

context
XmlSchemaObject

The schema information, such as the line number of the XML element.

schemas
XmlSchemas

The collection of schemas in the document.

importer
XmlSchemaImporter

The importer being used.

compileUnit
CodeCompileUnit

A CodeCompileUnit to which you can add CodeDOM structures to generate alternative code for the XSD.

mainNamespace
CodeNamespace

The current namespace for the element.

options
CodeGenerationOptions

A bitwise combination of the enumeration values that specify the setting options on the code compiler.

codeProvider
CodeDomProvider

A CodeDomProvider that is used to generate the new code.

Returns

The name of the CLR type that this maps to.

Examples

The example below shows a simple implementation of the ImportSchemaType(String, String, XmlSchemaObject, XmlSchemas, XmlSchemaImporter, CodeCompileUnit, CodeNamespace, CodeGenerationOptions, CodeDomProvider) method. The code checks the namespace and name. When the right values are found, it adds the name of a custom assembly ("Order.dll") that must be referenced in the generated code. It then adds a new namespace ("Microsoft.Samples") that is generated in the code.

C#
public override string ImportSchemaType(string name, string ns,
    XmlSchemaObject context, XmlSchemas schemas,
    XmlSchemaImporter importer,
    CodeCompileUnit compileUnit, CodeNamespace codeNamespace,
    CodeGenerationOptions options, CodeDomProvider codeGenerator)
    {
        if (name.Equals("Order") && ns.Equals("http://orders/"))
        {
            compileUnit.ReferencedAssemblies.Add("Order.dll");
            codeNamespace.Imports.Add
               (new CodeNamespaceImport("Microsoft.Samples"));
             return "Order";
        }
        return null;
    }

Remarks

In order to control the generation of the code, you must use the classes found in the System.CodeDom namespace. For more information, see Using the CodeDOM and CodeDOM Quick Reference.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1