XmlAttributes.XmlType Property
Assembly: System.Xml (in system.xml.dll)
/** @property */ public XmlTypeAttribute get_XmlType () /** @property */ public void set_XmlType (XmlTypeAttribute value)
public function get XmlType () : XmlTypeAttribute public function set XmlType (value : XmlTypeAttribute)
Property Value
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(); t.SerializeObject("XmlType.xml"); } // 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); } }
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
public class Transportation
{
public Car cars[];
} //Transportation
public class Car
{
public int id;
} //Car
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.SerializeObject("XmlType.xml");
} //main
// 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.set_TypeName("Autos");
// Set the XmlTypeAttribute to the XmlType property.
attrs.set_XmlType(xType);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(Car.class.ToType(), attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer =
new XmlSerializer(Transportation.class.ToType(), xOver);
return xSer;
} //CreateOverrider
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[] { c1, c2 };
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(fileName);
xSer.Serialize(writer, myTransportation);
} //SerializeObject
} //Test
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.