Condividi tramite


XmlElementAttribute.DataType Proprietà

Definizione

Ottiene o imposta il tipo di dati XSD (XML Schema Definition) dell'elemento XML generato da 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

Valore della proprietà

Tipo di dati XML Schema.

Eccezioni

Impossibile eseguire il mapping del tipo di dati XML Schema specificato a the.NET tipo di dati.

Esempio

Nell'esempio seguente viene serializzata una classe denominata Group che contiene un campo denominato ExtraInfo, che restituisce un oggetto ArrayList. Nell'esempio vengono valide due istanze di XmlElementAttribute al campo e vengono specificati valori diversi DataType per ogni istanza. Ogni istanza consente a di XmlSerializer serializzare i tipi specificati inseriti nella matrice.

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

Commenti

Nella tabella seguente sono elencati i tipi di dati semplici di XML Schema con their.NET equivalenti.

Per i tipi di dati e schema XMLbase64Binary, utilizzare una matrice di Byte strutture e applicare un XmlElementAttribute oggetto con il DataType valore impostato su "base64Binary" o "hexBinary", a seconda delle hexBinary esigenze. Per xml Schema time e date tipi di dati, utilizzare il DateTime tipo e applicare XmlElementAttribute con il DataType valore impostato su "date" o "time".

Per ogni tipo di XML Schema mappato a una stringa, applicare con XmlElementAttribute la relativa DataType proprietà impostata sul tipo di XML Schema. È possibile che questo possa modificare il formato di serializzazione, non solo lo schema per il membro.

Annotazioni

La proprietà fa distinzione tra maiuscole e minuscole, pertanto è necessario impostarla esattamente su uno dei tipi di dati XML Schema.

Annotazioni

Il passaggio di dati binari come elemento XML è più efficiente rispetto al passaggio come attributo XML Schema.

Per altre informazioni sui tipi di dati XML, vedere il documento World Wide Web Consortium denominato XML Schema Part 2: Datatypes.

Tipo di dati XSD Tipo di dati .NET
anyURI String
base64Binary Matrice di Byte oggetti
boolean Boolean
byte SByte
date DateTime
data e ora DateTime
decimale Decimal
doppio Double
ENTITÀ String
ENTITÀ String
galleggiare Single
gDay String
gMonth String
gMonthDay String
gYear String
AnnoMese String
Hexbinary Matrice di Byte oggetti
Documento d'identità String
IDREF String
IDREFS String
int Int32
numero intero String
language String
lungo Int64
Nome String
Ncname String
negativeInteger String
NMTOKEN String
NMTOKENS String
normalizedString String
nonNegativeInteger String
nonPositiveInteger String
NOTAZIONE String
positiveInteger String
QName XmlQualifiedName
duration String
string String
short Int16
Tempo DateTime
token String
byte senza segno Byte
intero non firmato UInt32
intero lungo non firmato UInt64
unsignedShort (numero breve senza segno) UInt16

Si applica a