Compartir a través de


XmlTextAttribute.DataType Propiedad

Definición

Obtiene o establece el tipo de datos del lenguaje de definición de esquemas XML (XSD) del texto generado por .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

Valor de propiedad

Un tipo de datos de esquema XML (XSD).

Excepciones

El tipo de datos esquema XML especificado no se puede asignar al tipo de datos .NET.

El tipo de datos esquema XML especificado no es válido para la propiedad y no se puede convertir al tipo de miembro.

Ejemplos

using System;
using System.Xml.Serialization;
using System.IO;

public class Group1{
   // The XmlTextAttribute with type set to string informs the
   // XmlSerializer that strings should be serialized as XML text.
   [XmlText(typeof(string))]
   [XmlElement(typeof(int))]
   [XmlElement(typeof(double))]
   public object [] All= new object []{321, "One", 2, 3.0, "Two" };
}

public class Group2{
   [XmlText(Type = typeof(GroupType))]
   public GroupType Type;
}
public enum GroupType{
   Small,
   Medium,
   Large
}

public class Group3{
   [XmlText(Type=typeof(DateTime))]
   public DateTime CreationTime = DateTime.Now;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeArray("XmlText1.xml");
      t.SerializeEnum("XmlText2.xml");
      t.SerializeDateTime("XmlText3.xml");
   }

   private void SerializeArray(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group1));
      Group1 myGroup1 = new Group1();

      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup1);
      writer.Close();
   }

   private void SerializeEnum(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group2));
      Group2 myGroup = new Group2();
      myGroup.Type = GroupType.Medium;
      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup);
      writer.Close();
   }

   private void SerializeDateTime(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group3));
      Group3 myGroup = new Group3();
      TextWriter writer = new StreamWriter(filename);

      ser.Serialize(writer, myGroup);
      writer.Close();
   }
}
Imports System.Xml.Serialization
Imports System.IO


Public Class Group1
   ' The XmlTextAttribute with type set to String informs the 
   ' XmlSerializer that strings should be serialized as XML text.
   <XmlText(GetType(String)), _
   XmlElement(GetType(integer)), _  
   XmlElement(GetType(double))> _
   public All () As Object = _
   New Object (){321, "One", 2, 3.0, "Two" }
End Class


Public Class Group2
   <XmlText(GetType(GroupType))> _
   public Type As GroupType 
End Class

Public Enum GroupType
   Small
   Medium
   Large
End Enum

Public Class Group3
   <XmlText(GetType(DateTime))> _
   Public CreationTime As DateTime = DateTime.Now
End Class

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
      t.SerializeArray("XmlText1.xml")
      t.SerializeEnum("XmlText2.xml")
      t.SerializeDateTime("XmlText3.xml")
   End Sub

   Private Sub SerializeArray(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group1))
      Dim myGroup1 As Group1 = New Group1()

      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup1)
      writer.Close()
   End Sub

   Private Sub SerializeEnum(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group2))
      Dim myGroup As Group2 = New Group2()
      myGroup.Type = GroupType.Medium
      Dim writer As TextWriter = New StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub

   Private Sub SerializeDateTime(filename As String)
      Dim ser As XmlSerializer = new XmlSerializer(GetType(Group3))
      Dim myGroup As Group3 = new Group3()
      Dim writer As TextWriter = new StreamWriter(filename)

      ser.Serialize(writer, myGroup)
      writer.Close()
   End Sub
End Class

Comentarios

Establecer la DataType propiedad en un tipo de datos simple de esquema XML afecta al formato del XML generado. Por ejemplo, establecer la propiedad en "date" hace que el texto generado tenga formato en el estilo de fecha general, por ejemplo: 2001-08-31. Por el contrario, establecer la propiedad en "dateTime" da como resultado un instante específico tal como lo define el documento 8601 de la Organización Internacional de Normalización, "Representaciones de fechas y horas", por ejemplo: 2001-08-15T06:59:11.0508456-07:00.

El efecto de establecer la DataType propiedad también se puede ver cuando se usa la Herramienta de definición de esquemas XML (Xsd.exe) para generar el esquema XML para un archivo compilado. Para obtener más información sobre el uso de la herramienta, vea La herramienta de definición de esquemas XML y la serialización XML.

En la tabla siguiente se enumeran los tipos de datos simples de esquema XML con sus equivalentes de .NET.

Para los tipos de datos y esquema base64Binary XML, use una matriz de Byte estructuras y aplique un XmlTextAttribute con el DataType establecido en "base64Binary" o "hexBinary", según hexBinary corresponda. Para los tipos de datos y date esquema time XML, use el DateTime tipo y aplique con XmlTextAttribute el DataType establecido en "fecha" o "hora".

Para cada tipo de datos de esquema XML asignado a una cadena, aplique con XmlTextAttribute su DataType propiedad establecida en el tipo de datos esquema XML. Tenga en cuenta que esto no cambia el formato de serialización, solo el esquema del miembro.

Nota:

La propiedad distingue mayúsculas de minúsculas, por lo que debe establecerla exactamente en uno de los tipos de datos de esquema XML.

Nota:

Pasar datos binarios como un elemento XML es más eficaz que pasarlos como un atributo XML.

Para obtener más información sobre los tipos de datos de esquema XML, vea el documento World Wide Web Consortium denominado Esquema XML Parte 2: Tipos de datos.

Tipo de datos XSD Tipo de datos de .NET
anyURI String
base64Binary Matriz de Byte objetos
booleano Boolean
byte SByte
date DateTime
fechaHora DateTime
Decimal Decimal
double Double
ENTITY String
ENTIDADES String
flotante Single
gDay String
gMonth String
gMonthDay String
gYear String
AñoMes String
hexBinary Matriz de Byte objetos
ID String
IDREF String
IDREFS String
int Int32
entero String
lenguaje String
largo Int64
Nombre String
NCName String
negativeInteger String
NMTOKEN String
NMTOKENS String
normalizedString String
nonNegativeInteger String
nonPositiveInteger String
NOTACIÓN String
positiveInteger String
QName XmlQualifiedName
duración String
cuerda / cadena String
short Int16
time DateTime
token String
byte sin signo Byte
unsignedInt UInt32
unsignedLong UInt64
entero corto sin signo UInt16

Se aplica a