XmlTextAttribute.DataType XmlTextAttribute.DataType XmlTextAttribute.DataType XmlTextAttribute.DataType Property

Definition

Ruft den XSD (XML Schema Definition)-Datentyp des von XmlSerializer generierten Textes ab oder legt diesen fest.Gets or sets the XML Schema definition language (XSD) data type of the text generated by the 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

Eigenschaftswert

Ein Datentyp für die XML-Schema (XSD).An XML Schema (XSD) data type.

Ausnahmen

Der angegebene XML-Schemadatentyp kann dem .NET-Datentyp nicht zugeordnet werden.The XML Schema data type you have specified cannot be mapped to the .NET data type.

Der angegebene XML-Schemadatentyp ist für die Eigenschaft nicht zulässig und kann nicht in den Membertyp konvertiert werden.The XML Schema data type you have specified is invalid for the property and cannot be converted to the member type.

Beispiele

#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
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

Hinweise

Festlegen der DataType Eigenschaft, um eine einfache XML-Schemadatentyps wirkt sich auf die generierte XML-Format.Setting the DataType property to an XML Schema simple data type affects the format of the generated XML. Festlegen der Eigenschaft "Datum" führt z. B. den generierten Text in das allgemeine Datumsformat, z. B. formatiert werden: 2001-08-31.For example, setting the property to "date" causes the generated text to be formatted in the general date style, for example: 2001-08-31. Im Gegensatz dazu, Festlegen der Eigenschaft auf "DateTime" Ergebnisse in bestimmten Zeitpunkt gemäß der International Organization for Standardization im Dokument 8601, "Darstellungen von Datum und Zeit", z.B.: 2001-08-15T06:59:11.0508456-07:00.By contrast, setting the property to "dateTime" results in a specific instant as defined by the International Organization for Standardization document 8601, "Representations of Dates and Times", for example: 2001-08-15T06:59:11.0508456-07:00.

Die Auswirkungen der Einstellung der DataType Eigenschaft kann auch angezeigt werden, wenn mit der XML Schema Definition Tool (Xsd.exe) zum Generieren des XML-Schemas für eine kompilierte Datei.The effect of setting the DataType property can also be seen when using the XML Schema Definition Tool (Xsd.exe) to generate the XML Schema for a compiled file. Weitere Informationen zur Verwendung dieses Tools finden Sie unter The XML Schema Definition-Tool und XML-Serialisierung.For more information on using the tool, see The XML Schema Definition Tool and XML Serialization.

Die folgende Tabelle enthält die einfachen XML-Schema-Datentypen mit ihren.The following table lists the XML Schema simple data types with their .NET equivalents.

Für die XML-Schema base64Binary und hexBinary Datentypen verwenden, ein Array von Byte Strukturen, und weisen eine XmlTextAttribute mit der DataType auf "base64Binary" oder "HexBinary" entsprechend festgelegt.For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a XmlTextAttribute with the DataType set to "base64Binary" or "hexBinary", as appropriate. Für die XML-Schema time und date Datentypen verwenden die DateTime und wenden Sie die XmlTextAttribute mit der DataType auf "Date" oder "Time" festgelegt.For the XML Schema time and date data types, use the DateTime type and apply the XmlTextAttribute with the DataType set to "date" or "time".

Für jede XML-Schemadatentyps, der in eine Zeichenfolge zugeordnet ist, gelten die XmlTextAttribute mit seiner DataType -Eigenschaft auf den XML-Schema-Datentyp festgelegt.For every XML Schema data type that is mapped to a string, apply the XmlTextAttribute with its DataType property set to the XML Schema data type. Beachten Sie, dass dies nicht das Serialisierungsformat, nur das Schema für das Element ändert.Note that this does not change the serialization format, only the schema for the member.

Hinweis

Die Eigenschaft ist Groß-/Kleinschreibung, sodass sie genau auf einen der XML-Schema-Datentypen festgelegt werden muss.The property is case-sensitive, so you must set it exactly to one of the XML Schema data types.

Hinweis

Übergeben von binären Daten als XML-ist Element effizienter als die Übergabe als XML-Attribut.Passing binary data as an XML element is more efficient than passing it as an XML attribute.

Weitere Informationen zu XML-Schema-Datentypen, finden Sie im World Wide Web Consortium-Dokument mit dem Namen XML Schema Part 2: Datentypen.For more information about XML Schema data types, see the World Wide Web Consortium document named XML Schema Part 2: Datatypes.

XSD-DatentypXSD data type Datentyp in .NET.NET data type
anyURIanyURI String
base64Binarybase64Binary Array von Byte ObjekteArray of Byte objects
booleanboolean Boolean
bytebyte SByte
datedate DateTime
dateTimedateTime DateTime
decimaldecimal Decimal
doubledouble Double
ENTITYENTITY String
ENTITÄTENENTITIES String
floatfloat Single
gDaygDay String
gMonthgMonth String
gMonthDaygMonthDay String
gYeargYear String
gYearMonthgYearMonth String
HexBinaryhexBinary Array von Byte ObjekteArray of Byte objects
IdID String
IDREFIDREF String
IDREFSIDREFS String
intint Int32
Ganze Zahlinteger String
languagelanguage String
longlong Int64
nameName String
NCNameNCName String
negativeIntegernegativeInteger String
NMTOKENNMTOKEN String
NMTOKENSNMTOKENS String
normalizedStringnormalizedString String
nonNegativeIntegernonNegativeInteger String
nonPositiveIntegernonPositiveInteger String
NOTATIONNOTATION String
positiveIntegerpositiveInteger String
QNameQName XmlQualifiedName
durationduration String
stringstring String
shortshort Int16
Uhrzeittime DateTime
tokentoken String
unsignedByteunsignedByte Byte
unsignedIntunsignedInt UInt32
unsignedLongunsignedLong UInt64
unsignedShortunsignedShort UInt16

Gilt für: