XmlTypeAttribute 類別

定義

控制由 XmlSerializer 序列化屬性 (Attribute) 目標後所產生的 XML 結構描述。Controls the XML schema that is generated when the attribute target is serialized by the XmlSerializer.

public ref class XmlTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)]
public class XmlTypeAttribute : Attribute
type XmlTypeAttribute = class
    inherit Attribute
Public Class XmlTypeAttribute
Inherits Attribute
繼承
XmlTypeAttribute
屬性

範例

下列範例顯示兩個已套用 XmlTypeAttribute 的類別。The following example shows two classes to which the XmlTypeAttribute has been applied.


[XmlType(Namespace="http://www.cpandl.com",
TypeName="GroupMember")]
public ref class Person
{
public:
   String^ Name;
};


[XmlType(Namespace="http://www.cohowinery.com",
TypeName="GroupAddress")]
public ref class Address
{
public:
   String^ Line1;
   String^ Line2;
   String^ City;
   String^ State;
   String^ Zip;
};

public ref class Group
{
public:
   array<Person^>^Staff;
   Person^ Manager;
   Address^ Location;
};

[XmlType(Namespace = "http://www.cpandl.com",
TypeName = "GroupMember")]
public class Person
{
   public string Name;
}

[XmlType(Namespace = "http://www.cohowinery.com",
TypeName = "GroupAddress")]
public class Address
{
   public string Line1;
   public string Line2;
   public string City;
   public string State;
   public string Zip;
}

public class Group
{
   public Person[] Staff;
   public Person Manager;
   public Address Location;
}

<XmlType(Namespace := "http://www.cpandl.com", _
 TypeName := "GroupMember")> _
Public Class Person
    Public Name As String
End Class

<XmlType(Namespace := "http://www.cohowinery.com", _
 TypeName := "GroupAddress")> _ 
Public Class Address
    
    Public Line1 As String
    Public Line2 As String
    Public City As String
    Public State As String
    Public Zip As String
End Class

Public Class Group
    Public Staff() As Person
    Public Manager As Person
    Public Location As Address
End Class

備註

XmlTypeAttribute 屬於控制 XmlSerializer 如何將物件序列化或還原序列化的一系列屬性。The XmlTypeAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. 如需類似屬性的完整清單,請參閱控制 XML 序列化的屬性For a complete list of similar attributes, see Attributes That Control XML Serialization.

您可以將 XmlTypeAttribute 套用至類別、結構、列舉或介面宣告。You can apply the XmlTypeAttribute to a class, structure, enumeration, or interface declaration.

XmlTypeAttribute 套用至類別,以指定 XML 型別的命名空間、XML 型別名稱,以及是否要在 XML 架構檔中包含型別。Apply the XmlTypeAttribute to a class to specify the XML type's namespace, the XML type name, and whether to include the type in the XML schema document. 若要查看設定 XmlTypeAttribute 類別之屬性的結果,請將您的應用程式編譯為可執行檔或 DLL,並將產生的檔案傳遞至XML 架構定義工具(xsd.exe)To see the results of setting the properties of the XmlTypeAttribute class, compile your application as an executable or DLL, and pass the resulting file to the XML Schema Definition Tool (Xsd.exe). 此工具會寫入架構,包括型別定義。The tool writes the schema, including the type definition.

注意

如果您將 IncludeInSchema] 屬性設定為 [false,則XML 架構定義工具(xsd.exe)不會在架構中包含型別。If you set the IncludeInSchema property to false, the XML Schema Definition Tool (Xsd.exe) does not include the type in the schema. 根據預設,針對每個公用類別,XSD 工具會產生 complexType 和該型別的元素。By default, for each public class, the XSD tool generates a complexType and an element of that type.

如需使用屬性的詳細資訊,請參閱屬性For more information about using attributes, see Attributes.

注意

您可以使用程式碼中的文字 XmlType,而不是較長的 XmlTypeAttributeYou can use the word XmlType in your code instead of the longer XmlTypeAttribute.

建構函式

XmlTypeAttribute()

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

XmlTypeAttribute(String)

初始化 XmlTypeAttribute 類別的新執行個體,並指定 XML 型別的名稱。Initializes a new instance of the XmlTypeAttribute class and specifies the name of the XML type.

屬性

AnonymousType

取得或設定值,判斷產生的結構描述型別是否為 XSD 匿名型別。Gets or sets a value that determines whether the resulting schema type is an XSD anonymous type.

IncludeInSchema

取得或設定值,指出是否將型別包含在 XML 結構描述文件中。Gets or sets a value that indicates whether to include the type in XML schema documents.

Namespace

取得或設定 XML 型別的命名空間。Gets or sets the namespace of the XML type.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)
TypeName

取得或設定 XML 型別的名稱。Gets or sets the name of the XML type.

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

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

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

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

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱