XmlElementAttribute.DataType Propriété

Définition

Obtient ou définit le type de données XSD (XML Schema Definition) de l'élément XML généré par 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

Valeur de propriété

String

Type de données de schéma XML.

Exceptions

Le type de données de schéma XML que vous avez spécifié ne peut pas être mappé au type de données .NET.

Exemples

L’exemple suivant sérialise une classe nommée Group qui contient un champ nommé ExtraInfo, qui retourne un ArrayList. L’exemple applique deux instances du XmlElementAttribute champ et spécifie des valeurs différentes DataType pour chaque instance. Chaque instance permet de XmlSerializer sérialiser les types spécifiés insérés dans le tableau.

#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

Remarques

Le tableau suivant répertorie les types de données simples de schéma XML avec their.NET équivalents.

Pour les types de données et hexBinary schéma base64Binary XML, utilisez un tableau de Byte structures et appliquez-le XmlElementAttribute avec la DataType valeur « base64Binary » ou « hexBinary », selon le cas. Pour les types de données et schéma time XML, utilisez le type et appliquez-le DateTime XmlElementAttribute avec la DataType valeur « date » ou « date heure ».

Pour chaque type de schéma XML mappé à une chaîne, appliquez la XmlElementAttribute propriété définie DataType sur le type de schéma XML. Il est possible que cela puisse modifier le format de sérialisation, pas seulement le schéma du membre.

Notes

La propriété respecte la casse. Vous devez donc la définir exactement sur l’un des types de données de schéma XML.

Notes

Le passage de données binaires en tant qu’élément XML est plus efficace que de le transmettre en tant qu’attribut de schéma XML.

Pour plus d’informations sur les types de données XML, consultez le document World Wide Web Consortium nommé XML Schema Part 2 : Datatypes.

Type de données XSD Type de données .NET
anyURI String
base64Binary Tableau d’objets Byte
boolean Boolean
byte SByte
date DateTime
dateTime DateTime
Décimal Decimal
double Double
ENTITY String
ENTITÉS String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
hexBinary Tableau d’objets Byte
id String
IDREF String
IDREFS String
int Int32
entier String
langage 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

S’applique à