This documentation is archived and is not being maintained.

XmlSerializer Constructor (Type, XmlAttributeOverrides, array<Type>, XmlRootAttribute, String)

Initializes a new instance of the XmlSerializer class that can serialize objects of type Object into XML document instances, and deserialize XML document instances into objects of type Object. Each object to be serialized can itself contain instances of classes, which this overload overrides with other classes. This overload also specifies the default namespace for all the XML elements and the class to use as the XML root element.

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

public:
XmlSerializer(
	Type^ type, 
	XmlAttributeOverrides^ overrides, 
	array<Type^>^ extraTypes, 
	XmlRootAttribute^ root, 
	String^ defaultNamespace
)

Parameters

type
Type: System::Type

The type of the object that this XmlSerializer can serialize.

overrides
Type: System.Xml.Serialization::XmlAttributeOverrides

An XmlAttributeOverrides that extends or overrides the behavior of the class specified in the type parameter.

extraTypes
Type: array<System::Type>

A Type array of additional object types to serialize.

root
Type: System.Xml.Serialization::XmlRootAttribute

An XmlRootAttribute that defines the XML root element properties.

defaultNamespace
Type: System::String

The default namespace of all XML elements in the XML document.

The overrides parameter allows for the creation of an XmlSerializer that serializes a class that extends or overrides the behavior of a base class. For example, given a DLL, it is possible to create a class that inherits or extends a class contained in the DLL. To serialize such a class, you must use an instance of the XmlAttributeOverrides class when constructing the XmlSerializer. For more details, see XmlAttributeOverrides.

By default, if a public property or field returns an object, or array of objects, the object types are automatically serialized. However, if a class contains a field or property that returns an array of type Object, any object can be inserted into that array. In that case, the XmlSerializer must be instructed to expect all the possible object types that are inserted into the Object array. To do this, use the extraTypes parameter to specify the extra object types to serialize or deserialize.

The root element of an XML document encloses all the other elements. By default, the object specified by the type parameter is serialized as the root element. Properties, such as the XML element name of the root element are taken from the type object. However, the root parameter allows you to replace the default object's information by specifying an XmlRootAttribute; the object allows you to set a different namespace, element name, and so on.

Use the defaultName parameter to specify the default namespace of all XML elements generated by the XmlSerializer.

The following example serializes an instance of a class that is defined in a DLL and to do so, overrides the public members found in the class. The example also specifies an array of extra types, the default namespace for all XML elements, and the class to use that provides the XML root element information. The example assumes that the code at the beginning has been compiled into a DLL named HighSchool.

// Beginning of the HighSchool.dll 
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

namespace HighSchool
{
   public ref class Student
   {
   public:
      String^ Name;
      int ID;
   };

   public ref class MyClass
   {
   public:
      array<Student^>^Students;
   };
}

namespace College
{

using namespace HighSchool;
   public ref class Graduate: public HighSchool::Student
   {
   public:
      Graduate(){}

      // Add a new field named University.
      String^ University;

      // Use extra types to use this field. 
      array<Object^>^Info;
   };

   public ref class Address
   {
   public:
      String^ City;
   };

   public ref class Phone
   {
   public:
      String^ Number;
   };

   public ref class Run
   {
   public:
      static void main()
      {
         Run^ test = gcnew Run;
         test->WriteOverriddenAttributes( "College.xml" );
         test->ReadOverriddenAttributes( "College.xml" );
      }

   private:
      void WriteOverriddenAttributes( String^ filename )
      {
         // Writing the file requires a TextWriter.
         TextWriter^ myStreamWriter = gcnew StreamWriter( filename );

         // Create an XMLAttributeOverrides class.
         XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;

         // Create the XmlAttributes class.
         XmlAttributes^ attrs = gcnew XmlAttributes;

         /* Override the Student class. "Alumni" is the name
               of the overriding element in the XML output. */
         XmlElementAttribute^ attr = gcnew XmlElementAttribute( "Alumni",Graduate::typeid );

         /* Add the XmlElementAttribute to the collection of
               elements in the XmlAttributes object. */
         attrs->XmlElements->Add( attr );

         /* Add the XmlAttributes to the XmlAttributeOverrides. 
               "Students" is the name being overridden. */
         attrOverrides->Add( HighSchool::MyClass::typeid, "Students", attrs );

         // Create array of extra types. 
         array<Type^>^extraTypes = gcnew array<Type^>(2);
         extraTypes[ 0 ] = Address::typeid;
         extraTypes[ 1 ] = Phone::typeid;

         // Create an XmlRootAttribute.
         XmlRootAttribute^ root = gcnew XmlRootAttribute( "Graduates" );

         /* Create the XmlSerializer with the 
               XmlAttributeOverrides object. */
         XmlSerializer^ mySerializer = gcnew XmlSerializer( HighSchool::MyClass::typeid,attrOverrides,extraTypes,root,"http://www.microsoft.com" );
         MyClass ^ myClass = gcnew MyClass;
         Graduate^ g1 = gcnew Graduate;
         g1->Name = "Jacki";
         g1->ID = 1;
         g1->University = "Alma";
         Graduate^ g2 = gcnew Graduate;
         g2->Name = "Megan";
         g2->ID = 2;
         g2->University = "CM";
         array<Student^>^myArray = {g1,g2};
         myClass->Students = myArray;

         // Create extra information.
         Address^ a1 = gcnew Address;
         a1->City = "Ionia";
         Address^ a2 = gcnew Address;
         a2->City = "Stamford";
         Phone^ p1 = gcnew Phone;
         p1->Number = "555-0101";
         Phone^ p2 = gcnew Phone;
         p2->Number = "555-0100";
         array<Object^>^o1 = {a1,p1};
         array<Object^>^o2 = {a2,p2};
         g1->Info = o1;
         g2->Info = o2;
         mySerializer->Serialize( myStreamWriter, myClass );
         myStreamWriter->Close();
      }

      void ReadOverriddenAttributes( String^ filename )
      {
         /* The majority of the code here is the same as that in the
               WriteOverriddenAttributes method. Because the XML being read
               doesn't conform to the schema defined by the DLL, the
               XMLAttributesOverrides must be used to create an 
               XmlSerializer instance to read the XML document.*/
         XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
         XmlAttributes^ attrs = gcnew XmlAttributes;
         XmlElementAttribute^ attr = gcnew XmlElementAttribute( "Alumni",Graduate::typeid );
         attrs->XmlElements->Add( attr );
         attrOverrides->Add( HighSchool::MyClass::typeid, "Students", attrs );
         array<Type^>^extraTypes = gcnew array<Type^>(2);
         extraTypes[ 0 ] = Address::typeid;
         extraTypes[ 1 ] = Phone::typeid;
         XmlRootAttribute^ root = gcnew XmlRootAttribute( "Graduates" );
         XmlSerializer^ readSerializer = gcnew XmlSerializer( HighSchool::MyClass::typeid,attrOverrides,extraTypes,root,"http://www.microsoft.com" );

         // A FileStream object is required to read the file. 
         FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
         MyClass ^ myClass;
         myClass = dynamic_cast<MyClass^>(readSerializer->Deserialize( fs ));

         /* Here is the difference between reading and writing an 
               XML document: You must declare an object of the derived 
               type (Graduate) and cast the Student instance to it.*/
         Graduate^ g;
         Address^ a;
         Phone^ p;
         System::Collections::IEnumerator^ myEnum = myClass->Students->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Graduate^ grad = safe_cast<Graduate^>(myEnum->Current);
            g = dynamic_cast<Graduate^>(grad);
            Console::Write( "{0}\t", g->Name );
            Console::Write( "{0}\t", g->ID );
            Console::Write( "{0}\n", g->University );
            a = dynamic_cast<Address^>(g->Info[ 0 ]);
            Console::WriteLine( a->City );
            p = dynamic_cast<Phone^>(g->Info[ 1 ]);
            Console::WriteLine( p->Number );
         }
      }
   };
}

int main()
{
   College::Run::main();
}
// Beginning of the HighSchool.dll 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization; 

namespace HighSchool
{
   public __gc class Student
   {
   public:
      String* Name;
      int ID;
   };

   public __gc class MyClass
   {
   public:
      Student* Students[];
   };
}

namespace College
{
   using namespace HighSchool;
public __gc class Graduate: public HighSchool::Student
{
public:
   Graduate(){}
   // Add a new field named University.
   String* University;
   // Use extra types to use this field.
   Object* Info[];
};

public __gc class Address
{
public:
   String* City;
};

public __gc class Phone
{
public:
   String* Number;
};

public __gc class Run
{
public:
   static void main()
   {
      Run* test = new Run();
      test->WriteOverriddenAttributes(S"College.xml");
      test->ReadOverriddenAttributes(S"College.xml");
   }

private:
   void WriteOverriddenAttributes(String* filename)
   {
      // Writing the file requires a TextWriter.
      TextWriter* myStreamWriter = new StreamWriter(filename);
      // Create an XMLAttributeOverrides class.
      XmlAttributeOverrides* attrOverrides = 
         new XmlAttributeOverrides();
      // Create the XmlAttributes class.
      XmlAttributes* attrs = new XmlAttributes();
      /* Override the Student class. "Alumni" is the name
      of the overriding element in the XML output. */

      XmlElementAttribute* attr = 
         new XmlElementAttribute(S"Alumni", __typeof(Graduate));
      /* Add the XmlElementAttribute to the collection of
      elements in the XmlAttributes object. */
      attrs->XmlElements->Add(attr);
      /* Add the XmlAttributes to the XmlAttributeOverrides. 
      "Students" is the name being overridden. */
      attrOverrides->Add(__typeof(HighSchool::MyClass), 
         S"Students", attrs);

      // Create array of extra types.
      Type* extraTypes[] = new Type*[2];
      extraTypes[0]=__typeof(Address);
      extraTypes[1]=__typeof(Phone);

      // Create an XmlRootAttribute.
      XmlRootAttribute* root = new XmlRootAttribute(S"Graduates");

      /* Create the XmlSerializer with the 
      XmlAttributeOverrides object. */
      XmlSerializer* mySerializer = new XmlSerializer
         (__typeof(HighSchool::MyClass), attrOverrides, extraTypes,
         root, S"http://www.microsoft.com");

      MyClass* myClass= new MyClass();

      Graduate* g1 = new Graduate();
      g1->Name = S"Jacki";
      g1->ID = 1;
      g1->University = S"Alma";

      Graduate* g2 = new Graduate();
      g2->Name = S"Megan";
      g2->ID = 2;
      g2->University = S"CM";

      Student* myArray[] = {g1,g2};

      myClass->Students = myArray;

      // Create extra information.
      Address* a1 = new Address();
      a1->City = S"Ionia";
      Address* a2 = new Address();
      a2->City = S"Stamford";
      Phone* p1 = new Phone();
      p1->Number = S"555-0101";
      Phone* p2 = new Phone();
      p2->Number = S"555-0100";

      Object* o1[] = {a1, p1};
      Object* o2[] = {a2,p2};

      g1->Info = o1;
      g2->Info = o2;
      mySerializer->Serialize(myStreamWriter,myClass);
      myStreamWriter->Close();
   }

private:
   void ReadOverriddenAttributes(String* filename)
   {
      /* The majority of the code here is the same as that in the
      WriteOverriddenAttributes method. Because the XML being read
      doesn't conform to the schema defined by the DLL, the
      XMLAttributesOverrides must be used to create an 
      XmlSerializer instance to read the XML document.*/

      XmlAttributeOverrides* attrOverrides = new 
         XmlAttributeOverrides();
      XmlAttributes* attrs = new XmlAttributes();
      XmlElementAttribute* attr = 
         new XmlElementAttribute(S"Alumni", __typeof(Graduate));
      attrs->XmlElements->Add(attr);
      attrOverrides->Add(__typeof(HighSchool::MyClass), 
         S"Students", attrs);

      Type* extraTypes[] = new Type*[2];
      extraTypes[0] = __typeof(Address);
      extraTypes[1] = __typeof(Phone);

      XmlRootAttribute* root = new XmlRootAttribute(S"Graduates");

      XmlSerializer* readSerializer = new XmlSerializer
         (__typeof(HighSchool::MyClass), attrOverrides, extraTypes,
         root, S"http://www.microsoft.com");

      // A FileStream object is required to read the file. 
      FileStream* fs = new FileStream(filename, FileMode::Open);

      MyClass* myClass;
      myClass = dynamic_cast<MyClass*> (readSerializer->Deserialize(fs));

      /* Here is the difference between reading and writing an 
      XML document: You must declare an object of the derived 
      type (Graduate) and cast the Student instance to it.*/
      Graduate* g;
      Address* a;
      Phone* p;
      System::Collections::IEnumerator* myEnum = myClass->Students->GetEnumerator();
      while (myEnum->MoveNext())
      {
         Graduate* grad = __try_cast<Graduate*>(myEnum->Current);
         g = dynamic_cast<Graduate*> (grad);
         Console::Write(S"{0}\t", g->Name);
         Console::Write(S"{0}\t", __box(g->ID));
         Console::Write(S"{0}\n", g->University);
         a = dynamic_cast<Address*> (g->Info[0]);
         Console::WriteLine(a->City);
         p = dynamic_cast<Phone*> (g->Info[1]);
         Console::WriteLine(p->Number);
      }
   }
};
}
int main()
{
   College::Run::main();
}

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
Show: