This documentation is archived and is not being maintained.

XmlAttributes.XmlType Property

Gets or sets an object that specifies how the XmlSerializer serializes a class to which the XmlTypeAttribute has been applied.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

public XmlTypeAttribute XmlType { get; set; }

Property Value

Type: System.Xml.Serialization.XmlTypeAttribute
An XmlTypeAttribute that overrides an XmlTypeAttribute applied to a class declaration.

The XmlTypeAttribute can be used to control how a type is serialized by the XmlSerializer. For example, by default, when a type is serialized, the XmlSerializer uses the class name as the XML element name. By creating an XmlTypeAttribute, setting the XmlType property to it, and creating an XmlAttributeOverrides object, you can change the XML element name.

The following example creates an XmlTypeAttribute object, and assigns it to the XmlType property of an XmlAttributes object.

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

public class Transportation
   public Car[] Cars;

public class Car
   public int ID;

public class Test
   public static void Main()
      Test t = new Test();

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
      // Create the XmlAttributes and XmlAttributeOverrides objects.
      XmlAttributes attrs = new XmlAttributes();
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();

      /* Create an XmlTypeAttribute and change the name of the 
         XML type. */
      XmlTypeAttribute xType = new XmlTypeAttribute();
      xType.TypeName = "Autos";

      // Set the XmlTypeAttribute to the XmlType property.
      attrs.XmlType = xType;

      /* Add the XmlAttributes to the XmlAttributeOverrides,
         specifying the member to override. */
      xOver.Add(typeof(Car), attrs);

      // Create the XmlSerializer, and return it.
      XmlSerializer xSer = new XmlSerializer
      (typeof(Transportation), xOver);
      return xSer;

   public void SerializeObject(string filename)
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create object and serialize it.
      Transportation myTransportation = 
      new Transportation();

      Car c1 = new Car();
      c1.ID = 12;

      Car c2 = new Car();
      c2.ID = 44;

      myTransportation.Cars = new Car[2]{c1,c2};

      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.