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

Non è possibile eseguire il mapping del tipo di dati XML Schema al tipo di dati .NET.

Esempio

L'esempio seguente serializza una classe denominata Group che contiene un campo denominato ExtraInfo, che restituisce un oggetto ArrayList. Nell'esempio vengono applicati 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.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

Commenti

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

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

Per ogni tipo di XML Schema mappato a una stringa, applicare con la XmlElementAttribute 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.

Nota

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

Nota

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.For more information about XML data types, see the World Wide Web Consortium document named XML Schema Part 2: Datatypes.

Tipo di dati XSD Tipo di dati .NET
anyURI String
base64Binary Matrice di Byte oggetti
boolean Boolean
byte SByte
Data DateTime
dateTime DateTime
decimal Decimal
double Double
ENTITY String
ENTITÀ String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
Hexbinary Matrice di Byte oggetti
ID String
IDREF String
IDREFS String
INT Int32
numero intero String
Linguaggio String
long Int64
Nome 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

Si applica a