XmlElementAttribute.DataType Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur jenis data definisi Skema XML (XSD) dari elemen XML yang dihasilkan oleh 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
Nilai Properti
Jenis data Skema XML.
Pengecualian
Tipe data Skema XML yang telah Anda tentukan tidak dapat dipetakan ke tipe data the.NET.
Contoh
Contoh berikut menserialisasikan kelas bernama Group yang berisi bidang bernama ExtraInfo, yang mengembalikan ArrayList. Contoh ini menerapkan dua instans XmlElementAttribute ke bidang dan menentukan nilai yang berbeda DataType untuk setiap instans. Setiap instans memungkinkan untuk menserialisasikan XmlSerializer jenis yang ditentukan yang dimasukkan ke dalam array.
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
Keterangan
Tabel berikut mencantumkan tipe data sederhana Skema XML dengan their.NET setara.
Untuk Skema base64Binary XML dan hexBinary jenis data, gunakan array Byte struktur, dan terapkan XmlElementAttribute dengan DataType yang diatur ke "base64Binary" atau "hexBinary", sebagaimana mewajibkan. Untuk Skema time XML dan date jenis data, gunakan DateTime jenis dan terapkan XmlElementAttribute dengan DataType yang diatur ke "tanggal" atau "waktu".
Untuk setiap jenis Skema XML yang dipetakan ke string, terapkan XmlElementAttribute dengan propertinya DataType yang diatur ke jenis Skema XML. Ada kemungkinan bahwa ini dapat mengubah format serialisasi, tidak hanya skema untuk anggota.
Nota
Properti peka huruf besar/kecil, jadi Anda harus mengaturnya persis ke salah satu jenis data Skema XML.
Nota
Meneruskan data biner sebagai elemen XML lebih efisien daripada meneruskannya sebagai atribut Skema XML.
Untuk informasi selengkapnya tentang jenis data XML, lihat dokumen World Wide Web Consortium bernama XML Schema Bagian 2: Jenis data.
| Jenis data XSD | Jenis data .NET |
|---|---|
| anyURI | String |
| base64Binary | Byte Array objek |
| Boolean | Boolean |
| byte | SByte |
| date | DateTime |
| tanggalWaktu | DateTime |
| desimal | Decimal |
| dua kali lipat | Double |
| ENTITAS | String |
| ENTITAS | String |
| float | Single |
| gDay | String |
| gMonth | String |
| gMonthDay | String |
| gYear | String |
| gYearMonth | String |
| hexBinary | Byte Array objek |
| ID | String |
| IDREF | String |
| IDREFS | String |
| int (integer) | Int32 |
| bilangan bulat | String |
| bahasa | String |
| long | Int64 |
| Nama | String |
| NCName | String |
| bilangan bulat negatif | String |
| NMTOKEN | String |
| NMTOKENS | String |
| normalizedString | String |
| nonNegativeInteger | String |
| nonPositiveInteger | String |
| NOTASI | String |
| bilangan bulat positif | String |
| QName | XmlQualifiedName |
| duration | String |
| string | String |
| short | Int16 |
| waktu | DateTime |
| token | String |
| unsignedByte (byte tanpa tanda) | Byte |
| bilangan bulat tak bertanda | UInt32 |
| bilangan panjang tanpa tanda | UInt64 |
| bilangan pendek tanpa tanda | UInt16 |