Freigeben über


XmlElementAttribute.DataType-Eigenschaft

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

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Property DataType As String
'Usage
Dim instance As XmlElementAttribute
Dim value As String

value = instance.DataType

instance.DataType = value
public string DataType { get; set; }
public:
property String^ DataType {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_DataType ()

/** @property */
public void set_DataType (String value)
public function get DataType () : String

public function set DataType (value : String)

Eigenschaftenwert

Ein XML-Schemadatentyp laut Definition im Dokument "XML Schema Part 2: Datatypes" des World Wide Web Consortium (www.w3.org ).

Ausnahmen

Ausnahmetyp Bedingung

Exception

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

Hinweise

In der folgenden Tabelle sind die einfachen Datentypen des XML-Schemas und ihre Entsprechungen in .NET aufgelistet.

Verwenden Sie beim XML-Schemadatentyp base64Binary und hexBinary ein Array von Byte-Strukturen, und weisen Sie ein XmlElementAttribute zu, wobei DataType auf "base64Binary" bzw. "hexBinary" festgelegt wird. Verwenden Sie beim XML-Schemadatentyp time und date den DateTime-Typ, und weisen Sie das XmlElementAttribute zu, wobei DataType auf "date" oder "time" festgelegt ist.

Weisen Sie bei jedem einer Zeichenfolge zugeordneten XML-Schematyp das XmlElementAttribute zu, wobei die DataType-Eigenschaft auf den XML-Schematyp festgelegt ist. Möglicherweise wird dadurch nicht nur das Schema für den Member, sondern das Serialisierungsformat geändert.

Hinweis

Bei der Eigenschaft wird die Groß- und Kleinschreibung berücksichtigt, sodass diese genau auf einen der XML-Schemadatentypen festgelegt werden muss.

Hinweis

Das Übergeben von binären Daten als XML-Element ist effizienter als die Übergabe als XML-Schemaattribut.

Weitere Informationen über XML-Datentypen finden Sie im Dokument "XML Schema Part 2: Datatypes" des World Wide Web Consortium (www.w3.org).

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

ENTITIES

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

string

String

short

Int16

time

DateTime

token

String

unsignedByte

Byte

unsignedInt

UInt32

unsignedLong

UInt64

unsignedShort

UInt16

Beispiel

Im folgenden Beispiel wird die Group-Klasse serialisiert, die das ExtraInfo-Feld enthält, das ArrayList zurückgibt. Im Beispiel werden zwei Instanzen von XmlElementAttribute auf das Feld angewendet und unterschiedliche DataType-Werte für die einzelnen Instanzen angegeben. Jede Instanz ermöglicht es dem XmlSerializer, die in das Array eingefügten angegebenen Typen zu serialisieren.

Imports System
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
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();
   }
}
#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" );
}
import System.*;
import System.Collections.*;
import System.IO.*;
import 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. */
    /** @attribute XmlElement(DataType = "string",
        Type = String.class, Namespace = "http://www.cpandl.com")
        @attribute XmlElement(DataType = "int",
        Namespace = "http://www.cohowinery.com", Type = int.class)
     */
    public ArrayList extraInfo;
} //Group

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("ElementTypes.xml");
    } //main

    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(Group.class.ToType());
        // 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((Int32)100);
        // Serialize the object and close the TextWriter.
        s.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject
} //Run

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlElementAttribute-Klasse
XmlElementAttribute-Member
System.Xml.Serialization-Namespace