XmlTextAttribute.DataType Właściwość

Definicja

Pobiera lub ustawia typ danych języka definicji schematu XML (XSD) tekstu wygenerowanego XmlSerializerprzez element .

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

Wartość właściwości

String

Typ danych schematu XML (XSD).

Wyjątki

Nie można zamapować określonego typu danych schematu XML na typ danych platformy .NET.

Określony typ danych schematu XML jest nieprawidłowy dla właściwości i nie można go przekonwertować na typ elementu członkowskiego.

Przykłady

#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

Uwagi

Ustawienie właściwości na DataType prosty typ danych schematu XML wpływa na format wygenerowanego kodu XML. Na przykład ustawienie właściwości na "date" powoduje sformatowanie wygenerowanego tekstu w ogólnym stylu daty, na przykład: 2001-08-31. Natomiast ustawienie właściwości na "dateTime" powoduje utworzenie określonego momentu zdefiniowanego przez międzynarodową organizację standaryzacji dokumentu 8601, "Reprezentacje dat i godzin", na przykład: 2001-08-15T06:59:11.0508456-07:00.

Efekt ustawiania DataType właściwości można również zobaczyć przy użyciu narzędzia definicji schematu XML (Xsd.exe), aby wygenerować schemat XML dla skompilowanego pliku. Aby uzyskać więcej informacji na temat korzystania z narzędzia, zobacz Narzędzie definicji schematu XML i Serializacja XML.

W poniższej tabeli wymieniono proste typy danych schematu XML z odpowiednikami platformy .NET.

W przypadku typów schematów base64Binary i hexBinary danych XML należy użyć tablicy Byte struktur i zastosować XmlTextAttribute element z zestawem DataType "base64Binary" lub "hexBinary", odpowiednio. W przypadku typów schematu time i danych XML użyj DateTime typu i zastosuj XmlTextAttribute element z ustawionym DataType na wartość "date" lub "datetime".

Dla każdego typu danych schematu XML mapowanego na ciąg zastosuj XmlTextAttribute właściwość z właściwością DataType ustawioną na typ danych schematu XML. Należy pamiętać, że nie zmienia to formatu serializacji, tylko schemat elementu członkowskiego.

Uwaga

Właściwość jest rozróżniana wielkość liter, dlatego należy ustawić ją dokładnie na jeden z typów danych schematu XML.

Uwaga

Przekazywanie danych binarnych jako elementu XML jest bardziej wydajne niż przekazywanie go jako atrybutu XML.

Aby uzyskać więcej informacji na temat typów danych schematu XML, zobacz dokument World Wide Web Consortium o nazwie XML Schema Part 2: Datatypes (Schemat XML— część 2: typy danych).

Typ danych XSD Typ danych platformy .NET
anyURI String
Base64binary Tablica Byte obiektów
boolean Boolean
byte SByte
data DateTime
Data i godzina DateTime
decimal Decimal
double Double
JEDNOSTKI String
PODMIOTY String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
Hexbinary Tablica Byte obiektów
ID (Identyfikator) String
IDREF String
IDREFS String
int Int32
liczba całkowita String
language String
długi Int64
Nazwa String
NCName String
ujemna liczba całkowita String
NMTOKEN String
NMTOKENS String
normalizedString String
nonNegativeInteger String
nonPositiveInteger String
NOTACJA String
dodatnia liczba całkowita String
QName XmlQualifiedName
czas trwania String
ciąg String
short Int16
time DateTime
token String
unsignedByte Byte
Unsignedint UInt32
unsignedLong UInt64
unsignedShort UInt16

Dotyczy