Bagikan melalui


SoapElementAttribute.DataType Properti

Definisi

Mendapatkan atau mengatur jenis data bahasa definisi Skema XML (XSD) dari elemen XML yang dihasilkan.

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

Salah satu jenis data Skema XML.

Contoh

Contoh berikut menserialisasikan instans kelas bernama Transportation yang berisi bidang bernama Vehicle. SoapElementAttribute diterapkan ke bidang . Ketika bidang diserialisasikan, nama elemen XML adalah "Roda" alih-alih "Kendaraan". Metode SerializeOverride ini membuat SoapElementAttribute dan mengatur SoapElement properti dari ke SoapAttributesSoapElementAttribute. SoapAttributes ditambahkan ke yang SoapAttributeOverrides digunakan untuk membuat XmlTypeMapping. XmlSerializer dibangun dengan XmlTypeMapping, dan instans kelas kembali diserialisasikanTransportation. SoapElementAttribute Karena digunakan untuk mengambil alih serialisasi, nama elemen XML yang dihasilkan sekarang adalah "Truk" alih-alih "Roda".

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;
using System.Text;
public class Transportation
{
   // The SoapElementAttribute specifies that the
   // generated XML element name will be "Wheels"
   // instead of "Vehicle".
   [SoapElement("Wheels")]
   public string Vehicle;
   [SoapElement(DataType = "dateTime")]
   public DateTime CreationDate;
   [SoapElement(IsNullable = true)]
   public Thing thing;
}

public class Thing{
   [SoapElement(IsNullable=true)] public string ThingName;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("SoapElementOriginal.xml");
      t.SerializeOverride("SoapElementOverride.xml");
      Console.WriteLine("Finished writing two XML files.");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateSoapOverrider()
   {
      // Create the SoapAttributes and SoapAttributeOverrides objects.
      SoapAttributes soapAttrs = new SoapAttributes();

      SoapAttributeOverrides soapOverrides =
      new SoapAttributeOverrides();

      /* Create an SoapElementAttribute to override
      the Vehicles property. */
      SoapElementAttribute soapElement1 =
      new SoapElementAttribute("Truck");
      // Set the SoapElement to the object.
      soapAttrs.SoapElement= soapElement1;

      /* Add the SoapAttributes to the SoapAttributeOverrides,
      specifying the member to override. */
      soapOverrides.Add(typeof(Transportation), "Vehicle", soapAttrs);

      // Create the XmlSerializer, and return it.
      XmlTypeMapping myTypeMapping = (new SoapReflectionImporter
      (soapOverrides)).ImportTypeMapping(typeof(Transportation));
      return new XmlSerializer(myTypeMapping);
   }

   public void SerializeOverride(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer ser = CreateSoapOverrider();

      // Create the object and serialize it.
      Transportation myTransportation =
      new Transportation();

      myTransportation.Vehicle = "MyCar";
      myTransportation.CreationDate=DateTime.Now;
      myTransportation.thing = new Thing();

      XmlTextWriter writer =
      new XmlTextWriter(filename, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartElement("wrapper");
      ser.Serialize(writer, myTransportation);
      writer.WriteEndElement();
      writer.Close();
   }
   public void SerializeObject(string filename){
      // Create an XmlSerializer instance.
      XmlSerializer ser = new XmlSerializer(typeof(Transportation));
      Transportation myTransportation =
      new Transportation();
      myTransportation.Vehicle = "MyCar";
      myTransportation.CreationDate = DateTime.Now;
      myTransportation.thing = new Thing();
      XmlTextWriter writer =
      new XmlTextWriter(filename, Encoding.UTF8);
      writer.Formatting = Formatting.Indented;
      writer.WriteStartElement("wrapper");
      ser.Serialize(writer, myTransportation);
      writer.WriteEndElement();
      writer.Close();
   }
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Imports System.Text

Public Class Transportation
   ' The SoapElementAttribute specifies that the
   ' generated XML element name will be "Wheels"
   ' instead of "Vehicle".
   <SoapElement("Wheels")> Public Vehicle As String 
   <SoapElement(DataType:= "dateTime")> _
   public CreationDate As DateTime    
   <SoapElement(IsNullable:= true)> _
   public thing As Thing
End Class

Public Class Thing
   <SoapElement(IsNullable:=true)> public ThingName As string 
End Class

Public Class Test

   Shared Sub Main()
      Dim t As Test = New Test()
      t.SerializeObject("SoapElementOriginalVb.xml")
      t.SerializeOverride("SoapElementOverrideVb.xml")
      Console.WriteLine("Finished writing two XML files.")
   End Sub

   ' Return an XmlSerializer used for overriding.
   Public Function CreateSoapOverrider() As XmlSerializer 
      ' Create the SoapAttributes and SoapAttributeOverrides objects.
      Dim soapAttrs As SoapAttributes = New SoapAttributes()

      Dim soapOverrides As SoapAttributeOverrides = _
      New SoapAttributeOverrides()
            
      ' Create a SoapElementAttribute to override 
      ' the Vehicles property. 
      Dim soapElement1 As SoapElementAttribute = _
      New SoapElementAttribute("Truck")
      ' Set the SoapElement to the object.
      soapAttrs.SoapElement= soapElement1

      ' Add the SoapAttributes to the SoapAttributeOverrides,
      ' specifying the member to override. 
      soapOverrides.Add(GetType(Transportation), "Vehicle", soapAttrs)
      
      ' Create the XmlSerializer, and return it.
      Dim myTypeMapping As XmlTypeMapping = (New _
      SoapReflectionImporter (soapOverrides)).ImportTypeMapping _
      (GetType(Transportation))
      return New XmlSerializer(myTypeMapping)
   End Function

   Public Sub SerializeOverride(filename As String)
      ' Create an XmlSerializer instance.
      Dim ser As XmlSerializer = CreateSoapOverrider()

      ' Create the object and serialize it.
      Dim myTransportation As Transportation = _
      New Transportation()

      myTransportation.Vehicle = "MyCar"
      myTransportation.CreationDate = DateTime.Now
      myTransportation.thing= new Thing()
      
      Dim writer As XmlTextWriter = _
      New XmlTextWriter(filename, Encoding.UTF8)
      writer.Formatting = Formatting.Indented
      writer.WriteStartElement("wrapper")
      ser.Serialize(writer, myTransportation)
      writer.WriteEndElement()
      writer.Close()
   End Sub

   Public Sub SerializeObject(filename As String)
      ' Create an XmlSerializer instance.
      Dim ser As XmlSerializer = _
      New XmlSerializer(GetType(Transportation))
      
      Dim myTransportation As Transportation = _
      New Transportation()
      
      myTransportation.Vehicle = "MyCar"
      myTransportation.CreationDate=DateTime.Now
      myTransportation.thing= new Thing()

      Dim writer As XmlTextWriter = _
      new XmlTextWriter(filename, Encoding.UTF8)
      writer.Formatting = Formatting.Indented
      writer.WriteStartElement("wrapper")
      ser.Serialize(writer, myTransportation)
      writer.WriteEndElement()
      writer.Close()
   End Sub
End Class

Keterangan

Tabel berikut mencantumkan jenis data sederhana Skema XML dengan setara .NET mereka.

Untuk Skema base64Binary XML dan hexBinary jenis data, gunakan array Byte struktur, dan terapkan SoapElementAttribute dengan DataType yang diatur ke "base64Binary" atau "hexBinary", sebagaimana mewajibkan. Untuk Skema time XML dan date jenis data, gunakan DateTime jenis dan terapkan SoapElementAttribute dengan DataType yang diatur ke "tanggal" atau "waktu".

Untuk setiap jenis data Skema XML yang dipetakan ke string, terapkan SoapElementAttribute dengan propertinya DataType yang diatur ke jenis Skema XML. Perhatikan bahwa ini tidak mengubah format serialisasi, 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 XML.

Untuk informasi selengkapnya tentang jenis data XML, lihat dokumen World Wide Web Consortium, Skema XML 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

Berlaku untuk