XsdDataContractImporter Constructor (CodeCompileUnit)

 

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

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

public XsdDataContractImporter(
	CodeCompileUnit codeCompileUnit
)

Parameters

codeCompileUnit
Type: System.CodeDom.CodeCompileUnit

The CodeCompileUnit that will be used to store the code.

Use this constructor to add more code into a CodeCompileUnit that has already been generated.

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.

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();
    }
    else
    {
        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);
        sw.Close();
    }
}

.NET Framework
Available since 3.0
Return to top
Show: