XmlAttributeAttribute 類別

定義

指定 XmlSerializer 必須將類別成員序列化為 XML 屬性。Specifies that the XmlSerializer must serialize the class member as an XML attribute.

public ref 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
Public Class XmlAttributeAttribute
Inherits Attribute
繼承
XmlAttributeAttribute
屬性

範例

下列範例會序列化類別,其中包含要套用 XmlAttributeAttribute 的數個欄位。The 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 anySimpleType 類型的所有內建資料類型)的值(或值的陣列)。You 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. 如需 XSD 類型的清單及其對應的 to.NET 資料類型,請參閱 DataType 屬性。See the DataType property for a list of XSD types and how they are mapped to.NET data types.

您可以使用 XmlAttributeAttribute 來設定兩個特殊的屬性: xml: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 的淺層複本 (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)

適用於