共用方式為


XmlElementAttribute 類別

定義

表示當 XmlSerializer 串行化或還原串行化包含它的 物件時,公用欄位或屬性代表 XML 元素。

public ref class XmlElementAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlElementAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlElementAttribute = class
    inherit Attribute
Public Class XmlElementAttribute
Inherits Attribute
繼承
XmlElementAttribute
屬性

範例

以下範例將一個名為 GroupXmlElementAttribute 類別序列化,並將其套用到其多個成員上。 該欄位 Employees 會回傳一個物件 Employee 陣列。 在這種情況下,該 XmlElementAttribute 會指定產生的 XML 不會被巢狀(巢狀是陣列中項目的預設行為)。

using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;

public class Group
{
   /* Set the element name and namespace of the XML element.
   By applying an XmlElementAttribute to an array,  you instruct
   the XmlSerializer to serialize the array as a series of XML
   elements, instead of a nested set of elements. */

   [XmlElement(
   ElementName = "Members",
   Namespace = "http://www.cpandl.com")]
   public Employee[] Employees;

   [XmlElement(DataType = "double",
   ElementName = "Building")]
   public double GroupID;

   [XmlElement(DataType = "hexBinary")]
   public byte [] HexBytes;

   [XmlElement(DataType = "boolean")]
   public bool IsActive;

   [XmlElement(Type = typeof(Manager))]
   public Employee Manager;

   [XmlElement(typeof(int),
   ElementName = "ObjectNumber"),
   XmlElement(typeof(string),
   ElementName = "ObjectString")]
   public ArrayList ExtraInfo;
}

public class Employee
{
   public string Name;
}

public class Manager:Employee{
   public int Level;
}

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

   public void SerializeObject(string filename)
   {
      // Create the XmlSerializer.
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);

      /* Create an instance of the group to serialize, and set
         its properties. */
      Group group = new Group();
      group.GroupID = 10.089f;
      group.IsActive = false;

      group.HexBytes = new byte[1]{Convert.ToByte(100)};

      Employee x = new Employee();
      Employee y = new Employee();

      x.Name = "Jack";
      y.Name = "Jill";

      group.Employees = new Employee[2]{x,y};

      Manager mgr = new Manager();
      mgr.Name = "Sara";
      mgr.Level = 4;
      group.Manager = mgr;

      /* Add a number and a string to the
      ArrayList returned by the ExtraInfo property. */
      group.ExtraInfo = new ArrayList();
      group.ExtraInfo.Add(42);
      group.ExtraInfo.Add("Answer");

      // Serialize the object, and close the TextWriter.
      s.Serialize(writer, group);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      FileStream fs = new FileStream(filename, FileMode.Open);
      XmlSerializer x = new XmlSerializer(typeof(Group));
      Group g = (Group) x.Deserialize(fs);
      Console.WriteLine(g.Manager.Name);
      Console.WriteLine(g.GroupID);
      Console.WriteLine(g.HexBytes[0]);
      foreach(Employee e in g.Employees)
      {
         Console.WriteLine(e.Name);
      }
   }
}
Imports System.Collections
Imports System.IO
Imports System.Xml.Serialization


Public Class Group
    ' Set the element name and namespace of the XML element.
    <XmlElement(ElementName := "Members", _
     Namespace := "http://www.cpandl.com")> _    
    Public Employees() As Employee
    
    <XmlElement(DataType := "double", _
     ElementName := "Building")> _
    Public GroupID As Double
    
    <XmlElement(DataType := "hexBinary")> _
    Public HexBytes() As Byte
    
    <XmlElement(DataType := "boolean")> _
    Public IsActive As Boolean
    
    <XmlElement(GetType(Manager))> _
    Public Manager As Employee
    
    <XmlElement(GetType(Integer), _
        ElementName := "ObjectNumber"), _
     XmlElement(GetType(String), _
        ElementName := "ObjectString")> _
    Public ExtraInfo As ArrayList
End Class

Public Class Employee
    Public Name As String
End Class

Public Class Manager
    Inherits Employee
    Public Level As Integer
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("FirstDoc.xml")
        test.DeserializeObject("FirstDoc.xml")
    End Sub
    
    Public Sub SerializeObject(filename As String)
        ' Create the XmlSerializer.
        Dim s As New XmlSerializer(GetType(Group))
        
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the group to serialize, and set
        ' its properties. 
        Dim group As New Group()
        group.GroupID = 10.089f
        group.IsActive = False
        
        group.HexBytes = New Byte() {Convert.ToByte(100)}
        
        Dim x As New Employee()
        Dim y As New Employee()
        
        x.Name = "Jack"
        y.Name = "Jill"
        
        group.Employees = New Employee() {x, y}
        
        Dim mgr As New Manager()
        mgr.Name = "Sara"
        mgr.Level = 4
        group.Manager = mgr
        
        ' Add a number and a string to the
        ' ArrayList returned by the ExtraInfo property. 
        group.ExtraInfo = New ArrayList()
        group.ExtraInfo.Add(42)
        group.ExtraInfo.Add("Answer")
        
        ' Serialize the object, and close the TextWriter.      
        s.Serialize(writer, group)
        writer.Close()
    End Sub    
    
    Public Sub DeserializeObject(filename As String)
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim x As New XmlSerializer(GetType(Group))
        Dim g As Group = CType(x.Deserialize(fs), Group)
        Console.WriteLine(g.Manager.Name)
        Console.WriteLine(g.GroupID)
        Console.WriteLine(g.HexBytes(0))

        Dim e As Employee
        For Each e In g.Employees
            Console.WriteLine(e.Name)
        Next e
    End Sub
End Class

備註

屬於 XmlElementAttribute 一組屬性,控制物件如何 XmlSerializer 序列化或反序列化。 如需類似屬性的完整清單,請參閱 控制 XML 串行化的屬性

XML 文件通常包含 XML 元素,每個元素由三個部分組成:一個帶有可能屬性的開頭標籤、一個結尾標籤,以及標籤間的資料。 XML 標籤可以巢狀排列——也就是說,標籤間的資料也可以是 XML 元素。 一個元素能夠包圍另一個元素的能力,使文件能夠包含資料的階層結構。 XML 元素也可以包含屬性。

套用 XmlElementAttribute to public 欄位或公開讀寫屬性來控制 XML 元素的特性,例如元素名稱與命名空間。

可以 XmlElementAttribute 多次套用到回傳物件陣列的欄位。 其目的是透過屬性 Type 指定可插入陣列的不同類型。 例如,以下 C# 程式碼中的陣列同時接受字串與整數。

public class Things{
   [XmlElement(Type = typeof(string)),
   XmlElement(Type = typeof(int))]
   public object[] StringsAndInts;
}

這會產生可能類似以下的 XML 檔案。

<Things>
   <string>Hello</string>
   <int>999</int>
   <string>World</string>
</Things>

請注意,當你重複套用且 XmlElementAttribute 未指定 ElementName 屬性值時,元素名稱會依照可接受物件的型別命名。

如果你對回傳陣列的欄位或屬性套用 , XmlElementAttribute 陣列中的項目會以一連串 XML 元素編碼。

相反地,若未套用 an XmlElementAttribute 於此類欄位或屬性,陣列中的項目會以元素序列編碼,嵌套於以該欄位或屬性命名的元素之下。 (使用 XmlArrayAttribute and XmlArrayItemAttribute 屬性來控制陣列的序列化方式。)

你可以設定屬性 Type 指定一個類型,該類型是從原始欄位或屬性的類型衍生而來——也就是你套用 XmlElementAttribute的欄位或屬性。

如果欄位或屬性回傳 , ArrayList你可以對該成員套 XmlElementAttribute 用多個實例。 對每個實例,將屬性設定 Type 為可插入陣列的物件類型。

欲了解更多屬性的使用資訊,請參閱屬性。

備註

你可以在程式碼中使用這個詞 XmlElement ,而不是較長 XmlElementAttribute的 。

建構函式

名稱 Description
XmlElementAttribute()

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

XmlElementAttribute(String, Type)

初始化一個新的實例 XmlElementAttribute ,並指定 XML 元素的名稱及所應用成員 XmlElementAttribute 的派生型別。 此成員類型用於序列 XmlSerializer 化包含它的物件。

XmlElementAttribute(String)

初始化該類別的新實例 XmlElementAttribute ,並指定 XML 元素的名稱。

XmlElementAttribute(Type)

初始化該類別的新實例 XmlElementAttribute ,並指定套用 的 XmlElementAttribute 成員型別。 此型別用於 XmlSerializer 包含該類型的物件序列化或反序列化時。

屬性

名稱 Description
DataType

取得或設定由 XmlSerializer.

ElementName

取得或設定產生的 XML 元素名稱。

Form

取得或設定一個值,表示該元素是否被限定。

IsNullable

取得或設定一個值,指示是否 XmlSerializer 必須序列化一個 null 設定為空標籤且屬性 xsi:nil 設為 true的成員。

Namespace

取得或設定類別序列化時所產生的 XML 元素命名空間。

Order

取得或設定元素序列化或非序列化的明確順序。

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)

適用於

另請參閱