XmlAttributeAttribute 類別

定義

指定 XmlSerializer 必須將類別成員序列化為 XML 屬性。

public ref class XmlAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAttributeAttribute : 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
屬性

範例

下列範例會序列化類別,其中包含套用 的 XmlAttributeAttribute 數個欄位。

#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 。 如需類似屬性的完整清單,請參閱 控制 XML 序列化的屬性

套用至公用欄位或屬性時,會 XmlAttributeAttribute 通知 XmlSerializer 將成員序列化為 XML 屬性。 根據預設,會將 XmlSerializer 公用欄位和屬性序列化為 XML 元素。

您可以將唯一指派 XmlAttributeAttribute 給公用欄位或公用屬性,以傳回值 (或值的陣列,) 可以對應至其中一個 XML 架構定義語言 (XSD) 簡單類型, (包括從 XSD anySimpleType 類型) 衍生的所有內建資料類型。 可能的型別包括可對應至 XSD 簡單型別的任何類型,包括 GuidChar 和 列舉。 DataType如需 XSD 類型的清單,以及它們如何對應 to.NET 資料類型,請參閱 屬性。

有兩個可以使用 XmlAttributeAttribute 設定的特殊屬性: xml:lang (指定語言) ,而 xml:space (指定如何處理空白字元) 屬性。 這些屬性旨在傳達只與處理 XML 的應用程式相關的資訊。 設定這些範例會顯示在下列程式碼中。

[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  

如需使用屬性的詳細資訊,請參閱 屬性

注意

您可以在程式碼中使用字組 XmlAttribute ,而不是較長 XmlAttributeAttribute 的 。

建構函式

XmlAttributeAttribute()

初始化 XmlAttributeAttribute 類別的新執行個體。

XmlAttributeAttribute(String)

初始化 XmlAttributeAttribute 類別的新執行個體,並指定產生的 XML 屬性的名稱。

XmlAttributeAttribute(String, Type)

初始化 XmlAttributeAttribute 類別的新執行個體。

XmlAttributeAttribute(Type)

初始化 XmlAttributeAttribute 類別的新執行個體。

屬性

AttributeName

取得或設定 XML 屬性的名稱。

DataType

取得或設定由 XmlSerializer 產生之 XML 屬性的 XSD 資料型別。

Form

取得或設定值,指出 XmlSerializer 產生的 XML 屬性名稱是否為限定的。

Namespace

取得或設定 XML 屬性的 XML 命名空間。

Type

取得或設定 XML 屬性的複雜型別。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。

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

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於