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.
Public Overrides Function ImportSchemaType(ByVal name As String, ByVal ns As String, ByVal context As XmlSchemaObject, ByVal schemas As XmlSchemas, ByVal importer As XmlSchemaImporter, ByVal compileUnit As CodeCompileUnit, ByVal codeNamespace As CodeNamespace, ByVal options As CodeGenerationOptions, ByVal codeGenerator As CodeDomProvider) As String
If name.Equals("Order") AndAlso ns.Equals("http://orders/") Then
compileUnit.ReferencedAssemblies.Add("Order.dll")
codeNamespace.Imports.Add(New CodeNamespaceImport("Microsoft.Samples"))
Return "Order"
End If
Return Nothing
End Function 'ImportSchemaType
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;
}