Export (0) Print
Expand All

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 function get XmlType () : XmlTypeAttribute 
public function set XmlType (value : XmlTypeAttribute)

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.

No code example is currently available or this language may not be supported.
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc class Car
{
public:
   int ID;
};

public __gc class Transportation
{
public:
   Car* Cars[];
};

// Return an XmlSerializer used for overriding.
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 = S"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;
}

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;

   Car* temp0 [] = {c1,c2};
   myTransportation->Cars = temp0;

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

int main()
{
   SerializeObject(S"XmlType.xml");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft