Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
XmlSerializer Constructor (Type, XmlAttributeOverrides)

XmlSerializer Constructor (Type, XmlAttributeOverrides)

Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into objects of the specified type. Each object to be serialized can itself contain instances of classes, which this overload can override with other classes.

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

public XmlSerializer(
	Type type,
	XmlAttributeOverrides overrides
)

Parameters

type
Type: System.Type

The type of the object to serialize.

overrides
Type: System.Xml.Serialization.XmlAttributeOverrides

An XmlAttributeOverrides.

The overrides parameter can be used to control how fields and properties are encoded in XML. These settings override any attributes that already exist on the objects. This can be useful when the source code cannot be modified or multiple encodings are required for the same classes.

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 DLL.

// Beginning of HighSchool.dll 
namespace HighSchool
{
   public class Student
   {
      public string Name;
      public int ID;
   }

   public class MyClass
   {
      public Student[] Students;
   }
}

namespace College
   {
   using System;
   using System.IO;
   using System.Xml;
   using System.Xml.Serialization; 
   using HighSchool;

    public class Graduate:HighSchool.Student
    {
       public Graduate(){}
       // Add a new field named University. 
       public string University;
    }


   public class Run
   {
      public static void Main()
      {
         Run test = new Run();
         test.WriteOverriddenAttributes("College.xml");
         test.ReadOverriddenAttributes("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("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), 
         "Students", attrs);

         // Create the XmlSerializer. 
         XmlSerializer mySerializer = new XmlSerializer
         (typeof(HighSchool.MyClass), attrOverrides);

         MyClass myClass = new MyClass();

         Graduate g1 = new Graduate();
         g1.Name = "Jackie";
         g1.ID = 1;
         g1.University = "Alma Mater";

         Graduate g2 = new Graduate();
         g2.Name = "Megan";
         g2.ID = 2;
         g2.University = "CM";

         Student[] myArray = {g1,g2};
         myClass.Students = myArray;

         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("Alumni", typeof(Graduate));
         attrs.XmlElements.Add(attr);
         attrOverrides.Add(typeof(HighSchool.MyClass), 
         "Students", attrs);

         XmlSerializer readSerializer = new XmlSerializer
         (typeof(HighSchool.MyClass), attrOverrides);

         // To read the file, a FileStream object is required. 
         FileStream fs = new FileStream(filename, FileMode.Open);

         MyClass myClass;

         myClass = (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;

         foreach(Graduate grad in myClass.Students)
         {
            g = (Graduate) grad;
            Console.Write(g.Name + "\t");
            Console.Write(g.ID + "\t");
            Console.Write(g.University + "\n");
         }
      }
   }
}
// Beginning of 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;
};


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 the XmlSerializer. 
      XmlSerializer* mySerializer = new XmlSerializer
         (__typeof(HighSchool::MyClass), attrOverrides);

      MyClass* myClass = new MyClass();

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

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

      Student* myArray[] = {g1,g2};
      myClass->Students = myArray;

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

      XmlSerializer* readSerializer = new XmlSerializer
         (__typeof(HighSchool::MyClass), attrOverrides);

      // To read the file, a FileStream object is required. 
      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;

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

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

Community Additions

ADD
Show:
© 2015 Microsoft