Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XsdDataContractImporter Class

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

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

using System;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Globalization;

namespace XsdContractImporterExample
    class Program
        static void Main(string[] args)
                XmlSchemaSet schemas = Export();
                CodeCompileUnit ccu = Import(schemas);
                CompileCode(ccu, "Person.cs");
                CompileCode(ccu, "Person.vb");
            catch (Exception exc)
                Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace);
                Console.WriteLine("Press <Enter> to end....");


        static XmlSchemaSet Export()
            XsdDataContractExporter ex = new XsdDataContractExporter();
            return ex.Schemas;
        static CodeCompileUnit Import(XmlSchemaSet schemas)

            XsdDataContractImporter imp = new XsdDataContractImporter();

            // The EnableDataBinding option adds a RaisePropertyChanged method to 
            // the generated code. The GenerateInternal causes code access to be 
            // set to internal.
            ImportOptions iOptions = new ImportOptions();
            iOptions.EnableDataBinding = true;
            iOptions.GenerateInternal = true;
            imp.Options = iOptions;

            if (imp.CanImport(schemas))
                return imp.CodeCompileUnit;
                return null;
        static void CompileCode(CodeCompileUnit ccu, string sourceName)
            CodeDomProvider provider = null;
            FileInfo sourceFile = 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")
                provider = new Microsoft.CSharp.CSharpCodeProvider();
            else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
                provider = new Microsoft.VisualBasic.VBCodeProvider();
                Console.WriteLine("Source file must have a .cs or .vb extension");
            if (provider != null)
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                // Set code formatting options to your preference.
                options.BlankLinesBetweenMembers = true;
                options.BracingStyle = "C";

                StreamWriter sw = new StreamWriter(sourceName);
                provider.GenerateCodeFromCompileUnit(ccu, sw, options);

    public class Person
        public string FirstName;

        public string LastName;

        public Person(string newFName, string newLName)
            FirstName = newFName;
            LastName = newLName;


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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

© 2015 Microsoft