共用方式為


XmlElementAttribute.DataType 屬性

定義

取得或設定由 XmlSerializer.

public:
 property System::String ^ DataType { System::String ^ get(); void set(System::String ^ value); };
public string DataType { get; set; }
member this.DataType : string with get, set
Public Property DataType As String

屬性值

一種 XML 結構資料型別。

例外狀況

您指定的 XML Schema 資料型別無法映射到 the.NET 資料型別。

範例

以下範例序列化一個名為 Group 的類別,該類別包含一個名為 ExtraInfo的欄位,該欄位回傳 ArrayList。 範例中將兩個實例 XmlElementAttribute 應用於該欄位,並為每個實例指定不同的 DataType 值。 每個實例都能序列 XmlSerializer 化插入陣列中的指定類型。

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

public class Group
{
   /* Apply two XmlElementAttributes to the field. Set the DataType
      to string an int to allow the ArrayList to accept
      both types. The Namespace is also set to different values
      for each type. */
   [XmlElement(DataType = "string",
   Type = typeof(string),
   Namespace = "http://www.cpandl.com"),
   XmlElement(DataType = "int",
   Namespace = "http://www.cohowinery.com",
   Type = typeof(int))]
   public ArrayList ExtraInfo;
}

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

    public void SerializeObject(string filename)
    {
      // A TextWriter is needed to write the file.
      TextWriter writer = new StreamWriter(filename);

      // Create the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Group));

      // Create the object to serialize.
      Group myGroup = new Group();

      /* Add a string and an integer to the ArrayList returned
         by the ExtraInfo field. */
      myGroup.ExtraInfo = new ArrayList();
      myGroup.ExtraInfo.Add("hello");
      myGroup.ExtraInfo.Add(100);

      // Serialize the object and close the TextWriter.
      s.Serialize(writer,myGroup);
      writer.Close();
   }
}
Imports System.Collections
Imports System.IO
Imports System.Xml.Serialization


Public Class Group
    ' Apply two XmlElementAttributes to the field. Set the DataType
    ' to string and int to allow the ArrayList to accept
    ' both types. The Namespace is also set to different values
    ' for each type. 
    <XmlElement(DataType := "string", _
        Type := GetType(String), _
        Namespace := "http://www.cpandl.com"), _
     XmlElement(DataType := "int", _                    
        Type := GetType(Integer), _
        Namespace := "http://www.cohowinery.com")> _
    Public ExtraInfo As ArrayList
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("ElementTypes.xml")
    End Sub    
    
    Public Sub SerializeObject(filename As String)
        ' A TextWriter is needed to write the file.
        Dim writer As New StreamWriter(filename)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Create the object to serialize.
        Dim myGroup As New Group()
        
        ' Add a string and an integer to the ArrayList returned
        ' by the ExtraInfo field. 
        myGroup.ExtraInfo = New ArrayList()
        myGroup.ExtraInfo.Add("hello")
        myGroup.ExtraInfo.Add(100)
        
        ' Serialize the object and close the TextWriter.
        s.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

備註

下表列出了 XML Schema 簡單資料型別及其 their.NET 等價物。

對於 XML Schema base64BinaryhexBinary資料型態,請使用結構陣列Byte,並依情況將 a 與 集合 DataType 套用XmlElementAttribute到 “base64Binary” 或 “hexBinary”。 對於 XML Schema timedate 資料型別,請使用 該 DateTime 型別並將 套用 XmlElementAttribute ,並將 DataType 設定為「日期」或「時間」。

對於每個映射到字串的 XML Schema 類型,應將其XmlElementAttributeDataType屬性設定為 XML Schema 類型。 這可能會改變序列化格式,而不只是成員的結構。

備註

這個屬性是區分大小寫的,所以你必須精確地設定成 XML Schema 資料型別之一。

備註

將二進位資料作為 XML 元素傳遞,比以 XML Schema 屬性傳遞更有效率。

欲了解更多關於 XML 資料型別的資訊,請參閱萬維網聯盟(World Wide Web Consortium)文件《 XML Schema Part 2: Datatypes》。

XSD 數據類型 .NET 資料類型
anyURI String
base64二進位 物件陣 Byte
布爾值 Boolean
位元組 SByte
date DateTime
日期時間 DateTime
十進位 Decimal
雙倍 Double
實體 String
實體 String
float Single
gDay String
g月刊 String
g月日 String
gYear String
年份月份 String
六角二進位 物件陣 Byte
ID String
國際退伍軍人協會(IDREF) String
IDREFS String
int Int32
整數 String
語言 String
long Int64
名稱 String
NCName String
負整數 String
NMTOKEN String
NM代幣 String
normalizedString String
非負整數 String
非正整數 String
符號 String
正整數 String
QName XmlQualifiedName
duration String
字串 String
short Int16
time DateTime
Token String
無符號位元組 Byte
無符號整數 UInt32
無符號長整數 UInt64
無符號短整數 UInt16

適用於