共用方式為


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;
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 Schema 定義語言(XSD)的簡單型態之一(包括所有由 XSD anySimpleType 型別衍生出的內建資料型別)。 可能的類型包括任何可映射到 XSD 簡單型別的類型,包括 GuidChar、 和 列舉。 請參閱該屬性, DataType 了解 XSD 類型列表及其如何映射 to.NET 資料型別。

有兩個特殊屬性可用 XmlAttributeAttributexml: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的 。

建構函式

名稱 Description
XmlAttributeAttribute()

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

XmlAttributeAttribute(String, Type)

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

XmlAttributeAttribute(String)

初始化該類別的新實例 XmlAttributeAttribute ,並指定產生的 XML 屬性名稱。

XmlAttributeAttribute(Type)

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

屬性

名稱 Description
AttributeName

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

DataType

取得或設定由 XmlSerializer.

Form

取得或設定一個值,指示由 產生 XmlSerializer 的 XML 屬性名稱是否經過限定。

Namespace

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

Type

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

TypeId

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

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於