XsdDataContractImporter Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Allows the transformation of a set of XML schema files (.xsd) into common language runtime (CLR) types.

Namespace:   System.Runtime.Serialization
Assembly:  System.Runtime.Serialization (in System.Runtime.Serialization.dll)


Public Class XsdDataContractImporter


Initializes a new instance of the XsdDataContractImporter class.


Initializes a new instance of the XsdDataContractImporter class with the CodeCompileUnit that will be used to generate CLR code.


Gets a CodeCompileUnit used for storing the CLR types generated.


Gets or sets an ImportOptions that contains settable options for the import operation.


Gets a value that indicates whether the schemas contained in an XmlSchemaSet can be transformed into a CodeCompileUnit.

System_CAPS_pubmethodCanImport(XmlSchemaSet, ICollection(Of XmlQualifiedName))

Gets a value that indicates whether the specified set of types contained in an XmlSchemaSet can be transformed into CLR types generated into a CodeCompileUnit.

System_CAPS_pubmethodCanImport(XmlSchemaSet, XmlQualifiedName)

Gets a value that indicates whether the schemas contained in an XmlSchemaSet can be transformed into a CodeCompileUnit.

System_CAPS_pubmethodCanImport(XmlSchemaSet, XmlSchemaElement)

Gets a value that indicates whether a specific schema element contained in an XmlSchemaSet can be imported.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Returns a CodeTypeReference to the CLR type generated for the schema type with the specified XmlQualifiedName.

System_CAPS_pubmethodGetCodeTypeReference(XmlQualifiedName, XmlSchemaElement)

Returns a CodeTypeReference for the specified XML qualified element and schema element.


Serves as the default hash function. (Inherited from Object.)


Returns a list of CodeTypeReference objects that represents the known types generated when generating code for the specified schema type.


Gets the Type of the current instance.(Inherited from Object.)


Transforms the specified set of XML schemas contained in an XmlSchemaSet into a CodeCompileUnit.

System_CAPS_pubmethodImport(XmlSchemaSet, ICollection(Of XmlQualifiedName))

Transforms the specified set of schema types contained in an XmlSchemaSet into CLR types generated into a CodeCompileUnit.

System_CAPS_pubmethodImport(XmlSchemaSet, XmlQualifiedName)

Transforms the specified XML schema type contained in an XmlSchemaSet into a CodeCompileUnit.

System_CAPS_pubmethodImport(XmlSchemaSet, XmlSchemaElement)

Transforms the specified schema element in the set of specified XML schemas into a CodeCompileUnit and returns an XmlQualifiedName that represents the data contract name for the specified element.


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

Use the XsdDataContractImporter if you are creating a Web service that must interoperate with an existing Web service, or to create data contract types from XML schemas. XsdDataContractImporter will transform a set of XML schemas and create the .NET Framework types that represent the data contract in a selected programming language. To create the code, use the classes in the System.CodeDom namespace.

Conversely, use the XsdDataContractExporter class when you have created a Web service that incorporates data represented by CLR types and when you need to export XML schemas for each data type to be consumed by other Web services. That is, XsdDataContractExporter transforms a set of CLR types into a set of XML schemas.


To generate CLR types from XML schemas at design time, use the ServiceModel Metadata Utility Tool (Svcutil.exe). To generated code from XSD files, use the /dataContractOnly switch of the tool. You can also use the XsdDataContractImporter if you need more control over the process or need to do it programmatically.

The following example creates an XmlSchemaSet and calls the Import method to create a CodeCompileUnit. The CodeCompileUnit is then used to create both Visual C# and Visual Basic code files.

Imports System
Imports System.CodeDom.Compiler
Imports System.CodeDom
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Globalization

Class Program

    Shared Sub Main(ByVal args() As String) 
            Dim schemas As XmlSchemaSet = Export()
            Dim ccu As CodeCompileUnit = Import(schemas)
            CompileCode(ccu, "Person.cs")
            CompileCode(ccu, "Person.vb")
        Catch exc As Exception
            Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace)
            Console.WriteLine("Press <Enter> to end....")
        End Try

    End Sub 

    Shared Function Export() As XmlSchemaSet 
        Dim ex As New XsdDataContractExporter()
        Return ex.Schemas
    End Function
    Shared Function Import(ByVal schemas As XmlSchemaSet) As CodeCompileUnit 

        Dim imp As New XsdDataContractImporter()
       ' The EnableDataBinding option adds a RaisePropertyChanged method to
       ' the generated code. The GenerateInternal causes code access to be
       ' set to internal.
       Dim iOptions As New ImportOptions()
       iOptions.EnableDataBinding = true
       iOptions.GenerateInternal = true
       imp.Options = IOptions

        If imp.CanImport(schemas) Then
            Return imp.CodeCompileUnit
            Return Nothing
        End If
    End Function

    Shared Sub CompileCode(ByVal ccu As CodeCompileUnit, ByVal sourceName As String) 
        Dim provider As CodeDomProvider = Nothing
        Dim sourceFile As New FileInfo(sourceName)
        ' Select the code provider based on the input file extension, either C# or Visual Basic.
        If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS" Then
            provider = New Microsoft.CSharp.CSharpCodeProvider()
        ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" Then
            provider = New Microsoft.VisualBasic.VBCodeProvider()
            Console.WriteLine("Source file must have a .cs or .vb extension")
        End If
        If Not (provider Is Nothing) Then
            Dim options As New CodeGeneratorOptions()
            ' Set code formatting options to your preference. 
            options.BlankLinesBetweenMembers = True
            options.BracingStyle = "C"

            Dim sw As New StreamWriter(sourceName)
            provider.GenerateCodeFromCompileUnit(ccu, sw, options)
        End If

    End Sub
End Class

<DataContract()>  _
Public Class Person
    <DataMember()>  _
    Public FirstName As String

    <DataMember()>  _
    Public LastName As String

    Public Sub New(ByVal newFName As String, ByVal newLName As String) 
        FirstName = newFName
        LastName = newLName

    End Sub 
End Class 

.NET Framework
Available since 3.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top