XmlElementAttribute.DataType Eigenschaft

Definition

Ruft den XSD (XML Schema Definition)-Datentyp des vom XmlSerializer generierten XML-Elements ab oder legt diesen fest.

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 XML-Schemadatentyp.

Ausnahmen

Der angegebene XML-Schemadatentyp kann dem .NET-Datentyp nicht zugeordnet werden.

Beispiele

Im folgenden Beispiel wird eine Klasse namens Group serialisiert, die ein Feld mit dem Namen ExtraInfoenthält, das einen ArrayListzurückgibt. Im Beispiel werden zwei Instanzen von XmlElementAttribute auf das Feld angewendet und unterschiedliche DataType Werte für jede Instanz angegeben. Jede Instanz ermöglicht es, die XmlSerializer angegebenen Typen zu serialisieren, die in das Array eingefügt werden.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Group
{
public:

   /* 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=String::typeid,
   Namespace="http://www.cpandl.com"),
   XmlElement(DataType="snippet1>",
   Namespace="http://www.cohowinery.com",
   Type=Int32::typeid)]
   ArrayList^ ExtraInfo;
};

void SerializeObject( String^ filename )
{
   // A TextWriter is needed to write the file.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // Create the object to serialize.
   Group^ myGroup = gcnew Group;

   /* Add a string and an integer to the ArrayList returned
      by the ExtraInfo field. */
   myGroup->ExtraInfo = gcnew ArrayList;
   myGroup->ExtraInfo->Add( "hello" );
   myGroup->ExtraInfo->Add( 100 );

   // Serialize the object and close the TextWriter.
   s->Serialize( writer, myGroup );
   writer->Close();
}

int main()
{
   SerializeObject( "ElementTypes.xml" );
}
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

Hinweise

In der folgenden Tabelle sind die einfachen XML-Schema-Datentypen mit their.NET Entsprechungen aufgeführt.

Verwenden Sie für das XML-Schema base64Binary und hexBinary die Datentypen ein Array von Byte Strukturen, und wenden Sie nach Bedarf ein XmlElementAttribute an, wobei DataType "base64Binary" oder "hexBinary" festgelegt ist. Verwenden Sie für das XML-Schema time und date die Datentypen den DateTime Typ, und wenden Sie den XmlElementAttribute an, wobei auf DataType "Datum" oder "Uhrzeit" festgelegt ist.

Wenden Sie für jeden XML-Schematyp, der einer Zeichenfolge zugeordnet ist, die XmlElementAttributeDataType -Eigenschaft an, die auf den XML-Schematyp festgelegt ist. Es ist möglich, dass dies das Serialisierungsformat ändern kann, nicht nur das Schema für den Member.

Hinweis

Bei der Eigenschaft wird die Groß-/Kleinschreibung beachtet, sodass Sie sie genau auf einen der XML-Schemadatentypen festlegen müssen.

Hinweis

Das Übergeben von Binärdaten als XML-Element ist effizienter als die Übergabe als XML-Schema-Attribut.

Weitere Informationen zu XML-Datentypen finden Sie im Dokument des World Wide Web Consortium mit dem Namen XML Schema Part 2: Datatypes.

XSD-Datentyp .NET-Datentyp
anyURI String
base64Binary Array von Byte-Objekten
boolean Boolean
byte SByte
date DateTime
dateTime DateTime
Decimal Decimal
double Double
ENTITY String
ENTITÄTEN String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
hexBinary Array von Byte-Objekten
id String
IDREF String
IDREFS String
INT Int32
integer String
language String
long Int64
name String
NCName String
negativeInteger String
NMTOKEN String
NMTOKENS String
normalizedString String
nonNegativeInteger String
nonPositiveInteger String
NOTATION String
positiveInteger String
QName XmlQualifiedName
duration String
Zeichenfolge String
short Int16
time DateTime
token String
unsignedByte Byte
unsignedInt UInt32
unsignedLong UInt64
unsignedShort UInt16

Gilt für: