XmlElementAttribute.DataType Właściwość

Definicja

Pobiera lub ustawia typ danych definicji schematu XML (XSD) elementu XML 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.

Wyjątki

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

Przykłady

Poniższy przykład serializuje klasę o nazwie Group , która zawiera pole o nazwie ExtraInfo, które zwraca ArrayListwartość . W przykładzie do pola są stosowane dwa wystąpienia XmlElementAttribute obiektu i określa różne DataType wartości dla każdego wystąpienia. Każde wystąpienie umożliwia XmlSerializer serializacji określonych typów wstawionych do tablicy.

#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

Uwagi

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

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

Dla każdego typu schematu XML mapowanego na ciąg zastosuj XmlElementAttribute właściwość z właściwością DataType ustawioną na typ schematu XML. Możliwe, że może to zmienić format serializacji, nie 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 atrybut schematu XML.

Aby uzyskać więcej informacji na temat typów danych XML, zobacz dokument World Wide Web Consortium o nazwie XML Schema Part 2: Datatypes ( 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
Nazwa 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