XmlElementAttribute.DataType Property

Definition

Gets or sets the XML Schema definition (XSD) data type of the XML element generated by the XmlSerializer.

public string DataType { get; set; }

Property Value

An XML Schema data type.

Exceptions

The XML Schema data type you have specified cannot be mapped to the.NET data type.

Examples

The following example serializes a class named Group that contains a field named ExtraInfo, which returns an ArrayList. The example applies two instances of the XmlElementAttribute to the field and specifies different DataType values for each instance. Each instance enables the XmlSerializer to serialize the specified types inserted into the array.

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();
   }
}

Remarks

The following table lists the XML Schema simple data types with their.NET equivalents.

For the XML Schema base64Binary and hexBinary data types, use an array of Byte structures, and apply a XmlElementAttribute with the DataType set to "base64Binary" or "hexBinary", as appropriate. For the XML Schema time and date data types, use the DateTime type and apply the XmlElementAttribute with the DataType set to "date" or "time".

For every XML Schema type that is mapped to a string, apply the XmlElementAttribute with its DataType property set to the XML Schema type. It is possible that this can change the serialization format, not only the schema for the member.

Note

The property is case-sensitive, so you must set it exactly to one of the XML Schema data types.

Note

Passing binary data as an XML element is more efficient than passing it as an XML Schema attribute.

For more information about XML data types, see the World Wide Web Consortium document named XML Schema Part 2: Datatypes.

XSD data type .NET data type
anyURI String
base64Binary Array of Byte objects
boolean Boolean
byte SByte
date DateTime
dateTime DateTime
decimal Decimal
double Double
ENTITY String
ENTITIES String
float Single
gDay String
gMonth String
gMonthDay String
gYear String
gYearMonth String
hexBinary Array of Byte objects
ID String
IDREF String
IDREFS String
int Int32
integer String
language 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

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0