In this article

Including or Importing XML Schemas

In this article

An XML schema may contain <xs:import />, <xs:include />, and <xs:redefine /> elements. These schema elements refer to other XML schemas that can be used to supplement the structure of the schema that includes or imports them. The XmlSchemaImport, XmlSchemaInclude and XmlSchemaRedefine classes, map to these elements in the Schema Object Model (SOM) API.

Including or Importing an XML Schema

The following code example supplements the customer schema created in the Building XML Schemas topic with the address schema. Supplementing the customer schema with the address schema makes address types available in the customer schema.

The address schema can be incorporated using either <xs:include /> or <xs:import /> elements to use the components of the address schema as-is, or using an <xs:redefine /> element to modify any of its components to suit the need of the customer schema. Because the address schema has a targetNamespace that is different from that of the customer schema, the <xs:import /> element and therefore import semantics is used.

The code example includes the address schema in the following steps.

Adds the customer schema and the address schema to a new XmlSchemaSet object and then compiles them. Any schema validation warnings and errors encountered reading or compiling the schemas are handled by the ValidationEventHandler delegate.

Retrieves the compiled XmlSchema objects for both the customer and address schemas from the XmlSchemaSet by iterating over the Schemas property. Because the schemas are compiled, Post-Schema-Compilation-Infoset (PSCI) properties are accessible.

Creates an XmlSchemaImport object, sets the Namespace property of the import to the namespace of the address schema, sets the Schema property of the import to the XmlSchema object of the address schema, and adds the import to the Includes property of the customer schema.

Finally, recursively writes all of the schemas imported into the customer schema to the console using the Includes property of the customer schema. The Includes property provides access to all the includes, imports, or redefines added to a schema.

The following is the complete code example and the customer and address schemas written to the console.

Imports System
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaImportExample
Shared Sub Main()
' Add the customer and address schemas to a new XmlSchemaSet and compile them.
' Any schema validation warnings and errors encountered reading or
' compiling the schemas are handled by the ValidationEventHandler delegate.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add("http://www.tempuri.org", "customer.xsd")
schemaSet.Add("http://www.example.com/IPO", "address.xsd")
schemaSet.Compile()
' Retrieve the compiled XmlSchema objects for the customer and
' address schema from the XmlSchemaSet by iterating over
' the Schemas property.
Dim customerSchema As XmlSchema = Nothing
Dim addressSchema As XmlSchema = Nothing
For Each schema As XmlSchema In schemaSet.Schemas()
If schema.TargetNamespace = "http://www.tempuri.org" Then
customerSchema = schema
ElseIf schema.TargetNamespace = "http://www.example.com/IPO" Then
addressSchema = schema
End If
Next
' Create an XmlSchemaImport object, set the Namespace property
' to the namespace of the address schema, the Schema property
' to the address schema, and add it to the Includes property
' of the customer schema.
Dim import As XmlSchemaImport = New XmlSchemaImport()
import.Namespace = "http://www.example.com/IPO"
import.Schema = addressSchema
customerSchema.Includes.Add(import)
' Reprocess and compile the modified XmlSchema object
' of the customer schema and write it to the console.
schemaSet.Reprocess(customerSchema)
schemaSet.Compile()
customerSchema.Write(Console.Out)
' Recursively write all of the schemas imported into the
' customer schema to the console using the Includes
' property of the customer schema.
RecurseExternals(customerSchema)
End Sub
Shared Sub RecurseExternals(ByVal schema As XmlSchema)
For Each external As XmlSchemaExternal In Schema.Includes
If Not external.SchemaLocation = Nothing Then
Console.WriteLine("External SchemaLocation: {0}", external.SchemaLocation)
End If
If external.GetType() Is GetType(XmlSchemaImport) Then
Dim import As XmlSchemaImport = CType(external, XmlSchemaImport)
Console.WriteLine("Imported namespace: {0}", import.Namespace)
End If
If Not external.Schema Is Nothing Then
external.Schema.Write(Console.Out)
RecurseExternals(external.Schema)
End If
Next
End Sub
Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Else
If args.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
End If
End If
Console.WriteLine(args.Message)
End Sub
End Class