XmlAttributeAttribute 类

定义

指定 XmlSerializer 必须将类成员序列化为 XML 属性。Specifies that the XmlSerializer must serialize the class member as an XML attribute.

public ref class XmlAttributeAttribute : Attribute
public class XmlAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAttributeAttribute : Attribute
type XmlAttributeAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlAttributeAttribute = class
    inherit Attribute
Public Class XmlAttributeAttribute
Inherits Attribute
继承
XmlAttributeAttribute
属性

示例

下面的示例序列化一个类,其中包含要应用的多个字段 XmlAttributeAttributeThe following example serializes a class that contains several fields to which the XmlAttributeAttribute is applied.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;

public ref class Group
{
public:

   [XmlAttributeAttribute(Namespace="http://www.cpandl.com")]
   String^ GroupName;

   [XmlAttributeAttribute(DataType="base64Binary")]
   array<Byte>^GroupNumber;

   [XmlAttributeAttribute(DataType="date",AttributeName="CreationDate")]
   DateTime Today;
};

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid );

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   myGroup->GroupName = ".NET";
   array<Byte>^hexByte = {Convert::ToByte( 100 ),Convert::ToByte( 50 )};
   myGroup->GroupNumber = hexByte;
   DateTime myDate = DateTime(2001,1,10);
   myGroup->Today = myDate;

   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize( writer, myGroup );
   writer->Close();
}

int main()
{
   SerializeObject( "Attributes.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

public class Group
{
   [XmlAttribute (Namespace = "http://www.cpandl.com")]
   public string GroupName;

   [XmlAttribute(DataType = "base64Binary")]
   public Byte [] GroupNumber;

   [XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
   public DateTime Today;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("Attributes.xml");
   }

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =
      new XmlSerializer(typeof(Group));

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";

      Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
      Convert.ToByte(50)};
      myGroup.GroupNumber = hexByte;

      DateTime myDate = new DateTime(2001,1,10);
      myGroup.Today = myDate;

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
       writer.Close();
   }
}

Option Explicit
Option Strict

Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Schema


Public Class Group
    <XmlAttribute(Namespace := "http://www.cpandl.com")> _
        Public GroupName As String    
    <XmlAttribute(DataType := "base64Binary")> _
        Public GroupNumber() As Byte    
    <XmlAttribute(DataType := "date", AttributeName := "CreationDate")> _
        Public Today As DateTime
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Attributes.xml")
    End Sub 
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        myGroup.GroupName = ".NET"
        
        Dim hexByte() As Byte = {Convert.ToByte(100), Convert.ToByte(50)}
        myGroup.GroupNumber = hexByte
        
        Dim myDate As New DateTime(2001, 1, 10)
        myGroup.Today = myDate
        
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

注解

XmlAttributeAttribute属于控制 XmlSerializer 序列化或反序列化对象的方式的一系列属性。The XmlAttributeAttribute 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.

当应用于公共字段或属性时,会 XmlAttributeAttribute 通知将 XmlSerializer 成员序列化为 XML 特性。When applied to a public field or property, the XmlAttributeAttribute informs the XmlSerializer to serialize the member as an XML attribute. 默认情况下,将 XmlSerializer 公共字段和属性序列化为 XML 元素。By default, the XmlSerializer serializes public fields and properties as XML elements.

您可以将 XmlAttributeAttribute 仅分配给公共字段或公共属性,这些属性返回值 (或值数组) 这些值可以映射到 XML 架构定义语言 (xsd) 简单类型 (包括派生自 XSD 类型) 的所有内置数据类型 anySimpleTypeYou can assign the XmlAttributeAttribute only to public fields or public properties that return a value (or array of values) that can be mapped to one of the XML Schema definition language (XSD) simple types (including all built-in datatypes derived from the XSD anySimpleType type). 可能的类型包括可以映射到 XSD 简单类型的任何类型,包括 GuidChar 和枚举。The possible types include any that can be mapped to the XSD simple types, including Guid, Char, and enumerations. DataType有关 XSD 类型的列表以及如何将它们映射到 to.NET 数据类型,请参阅属性。See the DataType property for a list of XSD types and how they are mapped to.NET data types.

可以通过设置两个特殊属性 XmlAttributeAttributexml:lang (指定语言) 并 xml:space (指定如何处理空白) 特性。There are two special attributes that can be set with the XmlAttributeAttribute : the xml:lang (specifies language) and xml:space (specifies how to handle white space) attributes. 这些特性旨在传达与处理 XML 的应用程序相关的信息。These attributes are intended to convey information that is relevant only to an application processing the XML. 下面的代码演示了设置这些设置的示例。Examples of setting these are shown in the following code.

[XmlAttribute("xml:lang")]  
 public string Lang;  
 // Set this to 'default' or 'preserve'.  
 [XmlAttribute("space",   
 Namespace = "http://www.w3.org/XML/1998/namespace")]  
 public string Space 
<XmlAttribute("xml:lang")> _  
Public Lang As String   
' Set this to 'default' or 'preserve'.  
<XmlAttribute("space", _  
Namespace:= "http://www.w3.org/XML/1998/namespace")> _  
Public Space As String  

有关使用特性的详细信息,请参阅 特性For more information about using attributes, see Attributes.

备注

您可以 XmlAttribute 在代码中使用该单词而不是更长的时间 XmlAttributeAttributeYou can use the word XmlAttribute in your code instead of the longer XmlAttributeAttribute.

构造函数

XmlAttributeAttribute()

初始化 XmlAttributeAttribute 类的新实例。Initializes a new instance of the XmlAttributeAttribute class.

XmlAttributeAttribute(String)

初始化 XmlAttributeAttribute 类的新实例,并指定生成的 XML 属性的名称。Initializes a new instance of the XmlAttributeAttribute class and specifies the name of the generated XML attribute.

XmlAttributeAttribute(String, Type)

初始化 XmlAttributeAttribute 类的新实例。Initializes a new instance of the XmlAttributeAttribute class.

XmlAttributeAttribute(Type)

初始化 XmlAttributeAttribute 类的新实例。Initializes a new instance of the XmlAttributeAttribute class.

属性

AttributeName

获取或设置 XML 属性的名称。Gets or sets the name of the XML attribute.

DataType

获取或设置 XmlSerializer 生成的 XML 属性的 XSD 数据类型。Gets or sets the XSD data type of the XML attribute generated by the XmlSerializer.

Form

获取或设置一个值,该值指示 XmlSerializer 生成的 XML 属性名称是否是限定的。Gets or sets a value that indicates whether the XML attribute name generated by the XmlSerializer is qualified.

Namespace

获取或设置 XML 属性的 XML 命名空间。Gets or sets the XML namespace of the XML attribute.

Type

获取或设置 XML 属性的复杂类型。Gets or sets the complex type of the XML attribute.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

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)

适用于