XmlTextAttribute.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 bahasa definisi Skema XML (XSD) dari teks 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 (XSD).
Pengecualian
Tipe data Skema XML yang telah Anda tentukan tidak dapat dipetakan ke tipe data .NET.
Tipe data Skema XML yang Anda tentukan tidak valid untuk properti dan tidak dapat dikonversi ke tipe anggota.
Contoh
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::Xml::Serialization;
using namespace System::IO;
public ref class Group1
{
public:
// The XmlTextAttribute with type set to string informs the
// XmlSerializer that strings should be serialized as XML text.
[XmlText(String::typeid)]
[XmlElement(Int32::typeid)]
[XmlElement(Double::typeid)]
array<Object^>^All;
Group1()
{
array<Object^>^temp = {321,"One",2,3.0,"Two"};
All = temp;
}
};
public enum class GroupType
{
Small, Medium, Large
};
public ref class Group2
{
public:
[XmlText(Type=GroupType::typeid)]
GroupType Type;
};
public ref class Group3
{
public:
[XmlText(Type=DateTime::typeid)]
DateTime CreationTime;
Group3()
{
CreationTime = DateTime::Now;
}
};
public ref class Test
{
public:
static void main()
{
Test^ t = gcnew Test;
t->SerializeArray( "XmlText1.xml" );
t->SerializeEnum( "XmlText2.xml" );
t->SerializeDateTime( "XmlText3.xml" );
}
private:
void SerializeArray( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group1::typeid );
Group1^ myGroup1 = gcnew Group1;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup1 );
writer->Close();
}
void SerializeEnum( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group2::typeid );
Group2^ myGroup = gcnew Group2;
myGroup->Type = GroupType::Medium;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup );
writer->Close();
}
void SerializeDateTime( String^ filename )
{
XmlSerializer^ ser = gcnew XmlSerializer( Group3::typeid );
Group3^ myGroup = gcnew Group3;
TextWriter^ writer = gcnew StreamWriter( filename );
ser->Serialize( writer, myGroup );
writer->Close();
}
};
int main()
{
Test::main();
}
using System;
using System.Xml.Serialization;
using System.IO;
public class Group1{
// The XmlTextAttribute with type set to string informs the
// XmlSerializer that strings should be serialized as XML text.
[XmlText(typeof(string))]
[XmlElement(typeof(int))]
[XmlElement(typeof(double))]
public object [] All= new object []{321, "One", 2, 3.0, "Two" };
}
public class Group2{
[XmlText(Type = typeof(GroupType))]
public GroupType Type;
}
public enum GroupType{
Small,
Medium,
Large
}
public class Group3{
[XmlText(Type=typeof(DateTime))]
public DateTime CreationTime = DateTime.Now;
}
public class Test{
static void Main(){
Test t = new Test();
t.SerializeArray("XmlText1.xml");
t.SerializeEnum("XmlText2.xml");
t.SerializeDateTime("XmlText3.xml");
}
private void SerializeArray(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group1));
Group1 myGroup1 = new Group1();
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup1);
writer.Close();
}
private void SerializeEnum(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group2));
Group2 myGroup = new Group2();
myGroup.Type = GroupType.Medium;
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup);
writer.Close();
}
private void SerializeDateTime(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group3));
Group3 myGroup = new Group3();
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup);
writer.Close();
}
}
Imports System.Xml.Serialization
Imports System.IO
Public Class Group1
' The XmlTextAttribute with type set to String informs the
' XmlSerializer that strings should be serialized as XML text.
<XmlText(GetType(String)), _
XmlElement(GetType(integer)), _
XmlElement(GetType(double))> _
public All () As Object = _
New Object (){321, "One", 2, 3.0, "Two" }
End Class
Public Class Group2
<XmlText(GetType(GroupType))> _
public Type As GroupType
End Class
Public Enum GroupType
Small
Medium
Large
End Enum
Public Class Group3
<XmlText(GetType(DateTime))> _
Public CreationTime As DateTime = DateTime.Now
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.SerializeArray("XmlText1.xml")
t.SerializeEnum("XmlText2.xml")
t.SerializeDateTime("XmlText3.xml")
End Sub
Private Sub SerializeArray(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group1))
Dim myGroup1 As Group1 = New Group1()
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, myGroup1)
writer.Close()
End Sub
Private Sub SerializeEnum(filename As String)
Dim ser As XmlSerializer = New XmlSerializer(GetType(Group2))
Dim myGroup As Group2 = New Group2()
myGroup.Type = GroupType.Medium
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, myGroup)
writer.Close()
End Sub
Private Sub SerializeDateTime(filename As String)
Dim ser As XmlSerializer = new XmlSerializer(GetType(Group3))
Dim myGroup As Group3 = new Group3()
Dim writer As TextWriter = new StreamWriter(filename)
ser.Serialize(writer, myGroup)
writer.Close()
End Sub
End Class
Keterangan
DataType Mengatur properti ke jenis data sederhana Skema XML memengaruhi format XML yang dihasilkan. Misalnya, mengatur properti ke "tanggal" menyebabkan teks yang dihasilkan diformat dalam gaya tanggal umum, misalnya: 2001-08-31. Sebaliknya, mengatur properti ke "dateTime" menghasilkan instan tertentu seperti yang didefinisikan oleh dokumen Organisasi Internasional untuk Standardisasi 8601, "Representasi Tanggal dan Waktu", misalnya: 2001-08-15T06:59:11.0508456-07:00.
Efek pengaturan DataType properti juga dapat dilihat saat menggunakan Xml Schema Definition Tool (Xsd.exe) untuk menghasilkan Skema XML untuk file yang dikompilasi. Untuk informasi selengkapnya tentang menggunakan alat ini, lihat Alat Definisi Skema XML dan Serialisasi XML.
Tabel berikut ini mencantumkan tipe data sederhana Skema XML dengan setara .NET-nya.
Untuk Skema base64Binary
XML dan hexBinary
jenis data, gunakan array Byte struktur, dan terapkan XmlTextAttribute dengan DataType yang diatur ke "base64Binary" atau "hexBinary", sebagaimana mewajibkan. Untuk Skema time
XML dan date
jenis data, gunakan DateTime jenis dan terapkan XmlTextAttributeDataType dengan yang diatur ke "tanggal" atau "waktu".
Untuk setiap jenis data Skema XML yang dipetakan ke string, terapkan XmlTextAttribute dengan propertinya DataType yang diatur ke jenis data Skema XML. Perhatikan bahwa ini tidak mengubah format serialisasi, hanya skema untuk anggota.
Catatan
Properti peka huruf besar/kecil, jadi Anda harus mengaturnya persis ke salah satu jenis data Skema XML.
Catatan
Meneruskan data biner sebagai elemen XML lebih efisien daripada meneruskannya sebagai atribut XML.
Untuk informasi selengkapnya tentang jenis data Skema XML, lihat dokumen Konsorsium World Wide Web bernama Skema XML Bagian 2: Tipe data.
Jenis data XSD | Jenis data .NET |
---|---|
anyURI | String |
base64Binary | Array objek Byte |
boolean | Boolean |
byte | SByte |
tanggal | DateTime |
dateTime | DateTime |
decimal | Decimal |
double | Double |
ENTITAS | String |
ENTITAS | String |
float | Single |
gDay | String |
gMonth | String |
gMonthDay | String |
gYear | String |
gYearMonth | String |
hexBinary | Array objek Byte |
ID | String |
IDREF | String |
IDREFS | String |
int | 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 |
positiveInteger | String |
QName | XmlQualifiedName |
duration | String |
string | String |
short | Int16 |
waktu | DateTime |
token | String |
unsignedByte | Byte |
unsignedInt | UInt32 |
unsignedLong | UInt64 |
unsignedShort | UInt16 |