XsdDataContractImporter XsdDataContractImporter XsdDataContractImporter XsdDataContractImporter Class

定義

允許將 XML 結構描述檔案 (.xsd) 集合轉換為 Common Language Runtime (CLR) 型別。Allows the transformation of a set of XML schema files (.xsd) into common language runtime (CLR) types.

public ref class XsdDataContractImporter
public class XsdDataContractImporter
type XsdDataContractImporter = class
Public Class XsdDataContractImporter
繼承
XsdDataContractImporterXsdDataContractImporterXsdDataContractImporterXsdDataContractImporter

範例

下列範例會建立 XmlSchemaSet,然後呼叫 Import 方法來建立 CodeCompileUnitThe following example creates an XmlSchemaSet and calls the Import method to create a CodeCompileUnit. 接著,CodeCompileUnit 便可用來同時建立 Visual C# 和 Visual Basic 程式碼檔案。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)
        {
            try
            {
                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);
            }
            finally
            {
                Console.WriteLine("Press <Enter> to end....");
                Console.ReadLine();
            }

        }

        static XmlSchemaSet Export()
        {
            XsdDataContractExporter ex = new XsdDataContractExporter();
            ex.Export(typeof(Person));
            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))
            {
                imp.Import(schemas);
                return imp.CodeCompileUnit;
            }
            else
                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();
            }
            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();
            }
        }
    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName;

        [DataMember]
        public string LastName;

        public Person(string newFName, string newLName)
        {
            FirstName = newFName;
            LastName = newLName;
        }
    }
}
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) 
        Try
            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)
        Finally
            Console.WriteLine("Press <Enter> to end....")
            Console.ReadLine()
        End Try
    
    End Sub 
    
    Shared Function Export() As XmlSchemaSet 
        Dim ex As New XsdDataContractExporter()
        ex.Export(GetType(Person))
        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
            imp.Import(schemas)
            Return imp.CodeCompileUnit
        Else
            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()
        Else
            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)
            sw.Close()
        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 

備註

如果您要建立必須與現有 Web 服務相互操作的 Web 服務,或從 XML 結構描述建立資料合約型別,便可使用 XsdDataContractImporterUse 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 將轉換 XML 結構描述集合並建立 .NET Framework.NET Framework 型別,這些類別會表示採用所選定程式設計語言的資料合約。XsdDataContractImporter will transform a set of XML schemas and create the .NET Framework.NET Framework types that represent the data contract in a selected programming language. 為了建立程式碼,此時會使用 System.CodeDom 命名空間 (Namespace) 中的類別。To create the code, use the classes in the System.CodeDom namespace.

相對地,當您已建立合併 CLR 型別表示之資料的 Web 服務,以及當您需要匯出其他 Web 服務要取用之每個資料型別的 XML 結構描述時,則要使用 XsdDataContractExporter 類別。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. 也就是 XsdDataContractExporter 會將 CLR 型別集合轉換為 XML 結構描述集合。That is, XsdDataContractExporter transforms a set of CLR types into a set of XML schemas.

注意

若要在設計階段,從 XML 結構描述產生 CLR 型別,請使用ServiceModel Metadata Utility Tool (Svcutil.exe)To generate CLR types from XML schemas at design time, use the ServiceModel Metadata Utility Tool (Svcutil.exe). 若要從 XSD 檔案產生的程式碼,使用/dataContractOnly交換器的工具。To generated code from XSD files, use the /dataContractOnly switch of the tool. 如果需要更進一步控制處理序,或需要以程式設計的方式進行,您也可以使用 XsdDataContractImporterYou can also use the XsdDataContractImporter if you need more control over the process or need to do it programmatically.

建構函式

XsdDataContractImporter() XsdDataContractImporter() XsdDataContractImporter() XsdDataContractImporter()

初始化 XsdDataContractImporter 類別的新執行個體。Initializes a new instance of the XsdDataContractImporter class.

XsdDataContractImporter(CodeCompileUnit) XsdDataContractImporter(CodeCompileUnit) XsdDataContractImporter(CodeCompileUnit) XsdDataContractImporter(CodeCompileUnit)

使用將用來產生 CLR 程式碼的 XsdDataContractImporter,初始化 CodeCompileUnit 類別的新執行個體。Initializes a new instance of the XsdDataContractImporter class with the CodeCompileUnit that will be used to generate CLR code.

屬性

CodeCompileUnit CodeCompileUnit CodeCompileUnit CodeCompileUnit

取得用來儲存產生之 CLR 型別的 CodeCompileUnitGets a CodeCompileUnit used for storing the CLR types generated.

Options Options Options Options

取得或設定 ImportOptions,其中包含匯入作業的可設定選項。Gets or sets an ImportOptions that contains settable options for the import operation.

方法

CanImport(XmlSchemaSet) CanImport(XmlSchemaSet) CanImport(XmlSchemaSet) CanImport(XmlSchemaSet)

取得值,這個值會指出包含在 XmlSchemaSet 中的結構描述是否能轉換成 CodeCompileUnitGets a value that indicates whether the schemas contained in an XmlSchemaSet can be transformed into a CodeCompileUnit.

CanImport(XmlSchemaSet, ICollection<XmlQualifiedName>) CanImport(XmlSchemaSet, ICollection<XmlQualifiedName>) CanImport(XmlSchemaSet, ICollection<XmlQualifiedName>) CanImport(XmlSchemaSet, ICollection<XmlQualifiedName>)

取得值,這個值會指示包含在 XmlSchemaSet 的已指定型別集合是否能夠轉換為在 CodeCompileUnit 中產生的 CLR 型別。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.

CanImport(XmlSchemaSet, XmlQualifiedName) CanImport(XmlSchemaSet, XmlQualifiedName) CanImport(XmlSchemaSet, XmlQualifiedName) CanImport(XmlSchemaSet, XmlQualifiedName)

取得值,這個值會指出包含在 XmlSchemaSet 中的結構描述是否能轉換成 CodeCompileUnitGets a value that indicates whether the schemas contained in an XmlSchemaSet can be transformed into a CodeCompileUnit.

CanImport(XmlSchemaSet, XmlSchemaElement) CanImport(XmlSchemaSet, XmlSchemaElement) CanImport(XmlSchemaSet, XmlSchemaElement) CanImport(XmlSchemaSet, XmlSchemaElement)

取得值,這個值會指出是否能夠匯入包含在 XmlSchemaSet 的特定結構描述項目。Gets a value that indicates whether a specific schema element contained in an XmlSchemaSet can be imported.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetCodeTypeReference(XmlQualifiedName) GetCodeTypeReference(XmlQualifiedName) GetCodeTypeReference(XmlQualifiedName) GetCodeTypeReference(XmlQualifiedName)

CodeTypeReference 傳回給具有已指定 XmlQualifiedName 之結構描述型別所產生的 CLR 型別。Returns a CodeTypeReference to the CLR type generated for the schema type with the specified XmlQualifiedName.

GetCodeTypeReference(XmlQualifiedName, XmlSchemaElement) GetCodeTypeReference(XmlQualifiedName, XmlSchemaElement) GetCodeTypeReference(XmlQualifiedName, XmlSchemaElement) GetCodeTypeReference(XmlQualifiedName, XmlSchemaElement)

傳回指定之 XML 限定項目和結構描述項目的 CodeTypeReferenceReturns a CodeTypeReference for the specified XML qualified element and schema element.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetKnownTypeReferences(XmlQualifiedName) GetKnownTypeReferences(XmlQualifiedName) GetKnownTypeReferences(XmlQualifiedName) GetKnownTypeReferences(XmlQualifiedName)

傳回 CodeTypeReference 物件的清單,這份清單表示當產生指定之結構描述型別的程式碼時所產生的已知型別。Returns a list of CodeTypeReference objects that represents the known types generated when generating code for the specified schema type.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
Import(XmlSchemaSet) Import(XmlSchemaSet) Import(XmlSchemaSet) Import(XmlSchemaSet)

將包含在 XmlSchemaSet 的已指定 XML 結構描述集合轉換為 CodeCompileUnitTransforms the specified set of XML schemas contained in an XmlSchemaSet into a CodeCompileUnit.

Import(XmlSchemaSet, ICollection<XmlQualifiedName>) Import(XmlSchemaSet, ICollection<XmlQualifiedName>) Import(XmlSchemaSet, ICollection<XmlQualifiedName>) Import(XmlSchemaSet, ICollection<XmlQualifiedName>)

將包含在 XmlSchemaSet 的已指定結構描述型別集合轉換為在 CodeCompileUnit 中產生的 CLR 型別。Transforms the specified set of schema types contained in an XmlSchemaSet into CLR types generated into a CodeCompileUnit.

Import(XmlSchemaSet, XmlQualifiedName) Import(XmlSchemaSet, XmlQualifiedName) Import(XmlSchemaSet, XmlQualifiedName) Import(XmlSchemaSet, XmlQualifiedName)

將包含在 XmlSchemaSet 的已指定 XML 結構描述型別集合轉換為 CodeCompileUnitTransforms the specified XML schema type contained in an XmlSchemaSet into a CodeCompileUnit.

Import(XmlSchemaSet, XmlSchemaElement) Import(XmlSchemaSet, XmlSchemaElement) Import(XmlSchemaSet, XmlSchemaElement) Import(XmlSchemaSet, XmlSchemaElement)

將已指定 XML 結構描述集合中的已指定結構描述項目轉換為 CodeCompileUnit,然後傳回 XmlQualifiedName,其中表示所指定項目的資料合約名稱。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.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱