XsdDataContractExporter XsdDataContractExporter XsdDataContractExporter XsdDataContractExporter Class


允許將資料合約中使用的 .NET Framework 類型集合轉換成 XML 結構描述檔案 (.xsd)。Allows the transformation of a set of .NET Framework types that are used in data contracts into an XML schema file (.xsd).

public ref class XsdDataContractExporter
public class XsdDataContractExporter
type XsdDataContractExporter = class
Public Class XsdDataContractExporter


下列程式碼範例會建立 XsdDataContractExporter 的執行個體,然後呼叫 Export(Type) 方法。The following example creates an instance of the XsdDataContractExporter and calls the Export(Type) method.

using System;
using System.Xml;
using System.Runtime.Serialization;
using System.Xml.Schema;

public class Program
    public static void Main()
        catch (Exception exc)
            Console.WriteLine("Message: {0} StackTrace:{1}", exc.Message, exc.StackTrace);

    static void ExportXSD()
        XsdDataContractExporter exporter = new XsdDataContractExporter();
        if (exporter.CanExport(typeof(Employee)))
            Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count);
            XmlSchemaSet mySchemas = exporter.Schemas;

            XmlQualifiedName XmlNameValue = exporter.GetRootElementName(typeof(Employee));
            string EmployeeNameSpace = XmlNameValue.Namespace;

            foreach (XmlSchema schema in mySchemas.Schemas(EmployeeNameSpace))

    static void GetXmlElementName()
        XsdDataContractExporter myExporter = new XsdDataContractExporter();
        XmlQualifiedName xmlElementName =myExporter.GetRootElementName(typeof(Employee));
        Console.WriteLine("Namespace: {0}", xmlElementName.Namespace);
        Console.WriteLine("Name: {0}", xmlElementName.Name);
        Console.WriteLine("IsEmpty: {0}", xmlElementName.IsEmpty);

    [DataContract(Namespace = "www.Contoso.com/Examples/")]
    public class Employee
        public string EmployeeName;
        private string ID;
Imports System
Imports System.Collections
Imports System.Xml
Imports System.Runtime.Serialization
Imports System.Xml.Schema

Public Class Program
    Public Shared Sub Main() 
        Catch exc As Exception
            Console.WriteLine("Message: {0} StackTrace:{1}", exc.Message, exc.StackTrace)
        End Try
    End Sub 
    Shared Sub ExportXSD() 

        Dim exporter As New XsdDataContractExporter()

        ' Use the ExportOptions to add the Possessions type to the 
        ' collection of KnownTypes. 
        Dim eOptions As New ExportOptions()
        exporter.Options = eOptions

        If exporter.CanExport(GetType(Employee)) Then
            Console.WriteLine("number of schemas: {0}", exporter.Schemas.Count)
            Dim mySchemas As XmlSchemaSet = exporter.Schemas
            Dim XmlNameValue As XmlQualifiedName = _
            Dim EmployeeNameSpace As String = XmlNameValue.Namespace
            Dim schema As XmlSchema
            For Each schema In  mySchemas.Schemas(EmployeeNameSpace)
            Next schema
        End If
    End Sub 
    Shared Sub GetXmlElementName() 
        Dim myExporter As New XsdDataContractExporter()
        Dim xmlElementName As XmlQualifiedName = myExporter. _
        Console.WriteLine("Namespace: {0}", xmlElementName.Namespace)
        Console.WriteLine("Name: {0}", xmlElementName.Name)
        Console.WriteLine("IsEmpty: {0}", xmlElementName.IsEmpty)
    End Sub 
    <DataContract([Namespace] := "www.Contoso.com/Examples/")>  _
    Public Class Employee

        <DataMember()>  _
        Public EmployeeName As String
        <DataMember()>  _
        Private ID As String
        ' This member may return a Possessions type.
        <DataMember> _
        public Miscellaneous As Hashtable 

    End Class 

    <DataContract> _
    Public Class Possessions

        <DataMember> _
        Public ItemName As String
    End Class

End Class


當您已建立合併 Common Language Runtime (CLR) 型別表示之資料的 Web 服務,以及當您需要匯出其他 Web 服務要取用之每個型別的 XML 結構描述時,即可使用 XsdDataContractExporter 類別。Use the XsdDataContractExporter class when you have created a Web service that incorporates data represented by common language runtime (CLR) types and when you need to export XML schemas for each type to be consumed by other Web services. 也就是 XsdDataContractExporter 會將 CLR 型別集合轉換成 XML 結構描述 That is, XsdDataContractExporter transforms a set of CLR types into XML schemas. (如需可用類型的詳細資訊,請參閱Types Supported by the Data Contract Serializer。)這樣一來,這些結構描述就可以透過 Web 服務描述語言 (Web Services Description Language,WSDL) 文件公開,提供需要與您的服務相互操作之其他使用者加以使用。(For more information about the types that can be used, see Types Supported by the Data Contract Serializer.) The schemas can then be exposed through a Web Services Description Language (WSDL) document for use by others who need to interoperate with your service.

相反地,如果您要建立必須與現有 Web 服務相互操作的 Web 服務,則可使用 XsdDataContractImporter 來轉換 XML 結構描述,並且建立可表示選定程式設計語言內資料的 CLR 型別。Conversely, if you are creating a Web service that must interoperate with an existing Web service, use the XsdDataContractImporter to transform XML schemas and create the CLR types that represent the data in a selected programming language.

XsdDataContractExporter 會產生其中包含結構描述集合的 XmlSchemaSet 物件。The XsdDataContractExporter generates an XmlSchemaSet object that contains the collection of schemas. 透過 Schemas 屬性,存取結構描述的集合。Access the set of schemas through the Schemas property.


為了快速產生其他 Web 服務可以取用的 XML 結構描述定義 (XSD) 檔案,此時要使用 XsdDataContractExporterTo quickly generate XML schema definition (XSD) files that other Web services can consume, use the XsdDataContractExporter.

將結構描述匯出至 XmlSchemaSetExporting Schemas Into an XmlSchemaSet

若要建立其中包含 XML 結構描述檔案之 XmlSchemaSet 類別的執行個體,您應該留意下列事項。To create an instance of the XmlSchemaSet class that contains XML schema files, you should be aware of the following.

匯出的型別集合會記錄成為內部的資料合約集合。The set of types you are exporting are recorded as an internal set of data contracts. 因此您可以多次呼叫 CanExport 方法,在不會降低效能的情況下將新型別加入結構描述集合中,因為只有新型別會新增至該集合。Thus, you can call the CanExport method multiple times to add new types to the schema set without degrading performance because only the new types will be added to the set. Export 作業期間,現有的結構描述會跟新加入的結構描述進行比較。During the Export operation, the existing schemas are compared to the new schemas being added. 如果這時發生衝突,就會擲回例外狀況。If there are conflicts, an exception will be thrown. 通常在兩種型別使用相同的資料合約名稱,但是同一個 XsdDataContractExporter 執行個體卻匯出不同的合約 (即不同的成員) 時,就會偵測到衝突。A conflict is usually detected if two types with the same data contract name but different contracts (different members) are exported by the same XsdDataContractExporter instance.

使用匯出工具Using the Exporter

這個類別的建議使用方式如下:A recommended way of using this class is as follows:

  1. 使用其中一種 CanExport 多載,即可判定是否可以匯出指定的型別或型別集合。Use one of the CanExport overloads to determine whether the specified type or set of types can be exported. 使用符合您個人需求的一種多載。Use one of the overloads that is appropriate to your requirements.

  2. 呼叫對應的 Export 方法。Call the corresponding Export method.

  3. Schemas 屬性擷取結構描述。Retrieve the schemas from the Schemas property.


XsdDataContractExporter() XsdDataContractExporter() XsdDataContractExporter() XsdDataContractExporter()

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

XsdDataContractExporter(XmlSchemaSet) XsdDataContractExporter(XmlSchemaSet) XsdDataContractExporter(XmlSchemaSet) XsdDataContractExporter(XmlSchemaSet)

使用指定的結構描述集合,初始化 XsdDataContractExporter 類別的新執行個體。Initializes a new instance of the XsdDataContractExporter class with the specified set of schemas.


Options Options Options Options

取得或設定 ExportOptions,其中包含能夠針對匯出作業進行設定的選項。Gets or sets an ExportOptions that contains options that can be set for the export operation.

Schemas Schemas Schemas Schemas

取得已匯出之 XML 結構描述的集合。Gets the collection of exported XML schemas.


CanExport(ICollection<Assembly>) CanExport(ICollection<Assembly>) CanExport(ICollection<Assembly>) CanExport(ICollection<Assembly>)

取得值,這個值表示是否能夠匯出包含在組件集合中的 Common Language Runtime (CLR) 類型集合。Gets a value that indicates whether the set of .common language runtime (CLR) types contained in a set of assemblies can be exported.

CanExport(ICollection<Type>) CanExport(ICollection<Type>) CanExport(ICollection<Type>) CanExport(ICollection<Type>)

取得值,這個值表示是否能夠匯出包含在 ICollection<T> 中的 Common Language Runtime (CLR) 類型集合。Gets a value that indicates whether the set of .common language runtime (CLR) types contained in a ICollection<T> can be exported.

CanExport(Type) CanExport(Type) CanExport(Type) CanExport(Type)

取得值,這個值表示是否能夠匯出指定的 Common Language Runtime (CLR) 類型。Gets a value that indicates whether the specified common language runtime (CLR) type can be exported.

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

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

(Inherited from Object)
Export(ICollection<Assembly>) Export(ICollection<Assembly>) Export(ICollection<Assembly>) Export(ICollection<Assembly>)

轉換包含在指定組件集合中的類型。Transforms the types contained in the specified collection of assemblies.

Export(ICollection<Type>) Export(ICollection<Type>) Export(ICollection<Type>) Export(ICollection<Type>)

轉換已傳遞到這個方法之 ICollection<T> 中所包含的類型。Transforms the types contained in the ICollection<T> passed to this method.

Export(Type) Export(Type) Export(Type) Export(Type)

將指定的 .NET Framework.NET Framework 類型轉換成 XML 結構描述定義語言 (XSD) 結構描述。Transforms the specified .NET Framework.NET Framework type into an XML schema definition language (XSD) schema.

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

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

(Inherited from Object)
GetRootElementName(Type) GetRootElementName(Type) GetRootElementName(Type) GetRootElementName(Type)

傳回 Type 的最上層名稱和命名空間。Returns the top-level name and namespace for the Type.

GetSchemaType(Type) GetSchemaType(Type) GetSchemaType(Type) GetSchemaType(Type)

傳回指定類型的 XML 結構描述類型。Returns the XML schema type for the specified type.

GetSchemaTypeName(Type) GetSchemaTypeName(Type) GetSchemaTypeName(Type) GetSchemaTypeName(Type)

傳回 Type 的合約名稱以及合約命名空間。Returns the contract name and contract namespace for the Type.

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

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

(Inherited from Object)
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)