XmlTypeAttribute 类

定义

控制当属性目标由 XmlSerializer 序列化时生成的 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
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.Struct)>]
type XmlTypeAttribute = class
    inherit Attribute
Public Class XmlTypeAttribute
Inherits Attribute
继承
XmlTypeAttribute
属性

示例

下面的示例演示了已应用的两个类 XmlTypeAttributeThe 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.

备注

如果将属性设置 IncludeInSchemafalse ,则 (Xsd.exe) 的 XML 架构定义工具 不会在架构中包括该类型。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 的浅表副本。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)

将一组名称映射为对应的一组调度标识符。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)

适用于

另请参阅