XmlSerializer Constructor
This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Overload List
This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
[Visual Basic] Protected Sub New()
[C#] protected XmlSerializer();
[C++] protected: XmlSerializer();
[JScript] protected function XmlSerializer();
Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa.
[Visual Basic] Public Sub New(Type)
[C#] public XmlSerializer(Type);
[C++] public: XmlSerializer(Type*);
[JScript] public function XmlSerializer(Type);
Initializes an instance of the XmlSerializer class using an object that maps one type to another.
[Visual Basic] Public Sub New(XmlTypeMapping)
[C#] public XmlSerializer(XmlTypeMapping);
[C++] public: XmlSerializer(XmlTypeMapping*);
[JScript] public function XmlSerializer(XmlTypeMapping);
Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa. Specifies the default namespace for all the XML elements.
[Visual Basic] Public Sub New(Type, String)
[C#] public XmlSerializer(Type, string);
[C++] public: XmlSerializer(Type*, String*);
[JScript] public function XmlSerializer(Type, String);
Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa. If a property or field returns an array, the extraTypes parameter specifies objects that can be inserted into the array.
[Visual Basic] Public Sub New(Type, Type())
[C#] public XmlSerializer(Type, Type[]);
[C++] public: XmlSerializer(Type*, Type*[]);
[JScript] public function XmlSerializer(Type, Type[]);
Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa. Each object to be serialized can itself contain instances of classes, which this overload can override with other classes.
[Visual Basic] Public Sub New(Type, XmlAttributeOverrides)
[C#] public XmlSerializer(Type, XmlAttributeOverrides);
[C++] public: XmlSerializer(Type*, XmlAttributeOverrides*);
[JScript] public function XmlSerializer(Type, XmlAttributeOverrides);
Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and vice versa. Specifies the class to use as the XML root element.
[Visual Basic] Public Sub New(Type, XmlRootAttribute)
[C#] public XmlSerializer(Type, XmlRootAttribute);
[C++] public: XmlSerializer(Type*, XmlRootAttribute*);
[JScript] public function XmlSerializer(Type, XmlRootAttribute);
Initializes a new instance of the XmlSerializer class that can serialize objects of type Object into XML document instances, and vice versa. Each object to be serialized can itself contain instances of classes, which this overload can override 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.
[Visual Basic] Public Sub New(Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String)
[C#] public XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, string);
[C++] public: XmlSerializer(Type*, XmlAttributeOverrides*, Type*[], XmlRootAttribute*, String*);
[JScript] public function XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String);
Example
[Visual Basic, C#, C++] 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.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of the XmlSerializer constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 'Beginning of the HighSchool.dll Imports System Imports System.IO Imports System.Xml Imports System.Xml.Serialization Imports Microsoft.VisualBasic ' Imports HighSchool Namespace HighSchool Public Class Student Public Name As String Public ID As Integer End Class 'Student Public Class ClassRoom Public Students() As Student End Class 'ClassRoom End Namespace 'HighSchool Namespace College Public Class Graduate Inherits HighSchool.Student Public Sub New() End Sub 'New ' Add a new field named University. Public University As String ' Use extra types to use this field. Public Info() As Object End Class 'Graduate Public Class Address Public City As String End Class 'Address Public Class Phone Public Number As String End Class 'Phone Public Class Run Public Shared Sub Main() Dim test As New Run() test.WriteOverriddenAttributes("College.xml") test.ReadOverriddenAttributes("College.xml") End Sub 'Main Private Sub WriteOverriddenAttributes(ByVal filename As String) ' Writing the file requires a TextWriter. Dim myStreamWriter As New StreamWriter(filename) ' Create an XMLAttributeOverrides class. Dim attrOverrides As New XmlAttributeOverrides() ' Create the XmlAttributes class. Dim attrs As New XmlAttributes() ' Override the Student class. "Alumni" is the name ' of the overriding element in the XML output. Dim attr As New XmlElementAttribute("Alumni", GetType(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(GetType(HighSchool.ClassRoom), "Students", attrs) ' Create array of extra types. Dim extraTypes(1) As Type extraTypes(0) = GetType(Address) extraTypes(1) = GetType(Phone) ' Create an XmlRootAttribute. Dim root As New XmlRootAttribute("Graduates") ' Create the XmlSerializer with the ' XmlAttributeOverrides object. Dim mySerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _ attrOverrides, extraTypes, root, "http://www.microsoft.com") Dim oMyClass As New HighSchool.ClassRoom() Dim g1 As New Graduate() g1.Name = "Jacki" g1.ID = 1 g1.University = "Alma" Dim g2 As New Graduate() g2.Name = "Megan" g2.ID = 2 g2.University = "CM" Dim myArray As HighSchool.Student() = {g1, g2} oMyClass.Students = myArray ' Create extra information. Dim a1 As New Address() a1.City = "Ionia" Dim a2 As New Address() a2.City = "Stamford" Dim p1 As New Phone() p1.Number = "000-0000" Dim p2 As New Phone() p2.Number = "111-1111" Dim o1() As Object = {a1, p1} Dim o2() As Object = {a2, p2} g1.Info = o1 g2.Info = o2 mySerializer.Serialize(myStreamWriter, oMyClass) myStreamWriter.Close() End Sub 'WriteOverriddenAttributes Private Sub ReadOverriddenAttributes(ByVal filename As String) ' 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. Dim attrOverrides As New XmlAttributeOverrides() Dim attrs As New XmlAttributes() Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate)) attrs.XmlElements.Add(attr) attrOverrides.Add(GetType(HighSchool.ClassRoom), "Students", attrs) Dim extraTypes(1) As Type extraTypes(0) = GetType(Address) extraTypes(1) = GetType(Phone) Dim root As New XmlRootAttribute("Graduates") Dim readSerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _ attrOverrides, extraTypes, root, "http://www.microsoft.com") ' A FileStream object is required to read the file. Dim fs As New FileStream(filename, FileMode.Open) Dim oMyClass As HighSchool.ClassRoom oMyClass = CType(readSerializer.Deserialize(fs), HighSchool.ClassRoom) ' 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. Dim g As Graduate Dim a As Address Dim p As Phone Dim grad As Graduate For Each grad In oMyClass.Students g = CType(grad, Graduate) Console.Write((g.Name & ControlChars.Tab)) Console.Write((g.ID & ControlChars.Tab)) Console.Write((g.University & ControlChars.Cr)) a = CType(g.Info(0), Address) Console.WriteLine(a.City) p = CType(g.Info(1), Phone) Console.WriteLine(p.Number) Next grad End Sub 'ReadOverriddenAttributes End Class 'Run End Namespace 'College [C#] // Beginning of the 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; // Use extra types to use this field. public object[]Info; } public class Address { public string City; } public class Phone { public string Number; } 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 array of extra types. Type [] extraTypes = new Type[2]; extraTypes[0]=typeof(Address); extraTypes[1]=typeof(Phone); // Create an XmlRootAttribute. XmlRootAttribute root = new XmlRootAttribute("Graduates"); /* Create the XmlSerializer with the XmlAttributeOverrides object. */ XmlSerializer mySerializer = new XmlSerializer (typeof(HighSchool.MyClass), attrOverrides, extraTypes, root, "http://www.microsoft.com"); MyClass myClass= new MyClass(); Graduate g1 = new Graduate(); g1.Name = "Jacki"; g1.ID = 1; g1.University = "Alma"; Graduate g2 = new Graduate(); g2.Name = "Megan"; g2.ID = 2; g2.University = "CM"; Student[] myArray = {g1,g2}; myClass.Students = myArray; // Create extra information. Address a1 = new Address(); a1.City = "Ionia"; Address a2 = new Address(); a2.City = "Stamford"; Phone p1 = new Phone(); p1.Number = "000-0000"; Phone p2 = new Phone(); p2.Number = "111-1111"; Object[]o1 = new Object[2]{a1, p1}; Object[]o2 = new Object[2]{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("Alumni", typeof(Graduate)); attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(HighSchool.MyClass), "Students", attrs); Type [] extraTypes = new Type[2]; extraTypes[0] = typeof(Address); extraTypes[1] = typeof(Phone); XmlRootAttribute root = new XmlRootAttribute("Graduates"); XmlSerializer readSerializer = new XmlSerializer (typeof(HighSchool.MyClass), attrOverrides, extraTypes, root, "http://www.microsoft.com"); // A FileStream object is required to read the file. 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; Address a; Phone p; foreach(Graduate grad in myClass.Students) { g = (Graduate) grad; Console.Write(g.Name + "\t"); Console.Write(g.ID + "\t"); Console.Write(g.University + "\n"); a = (Address) g.Info[0]; Console.WriteLine(a.City); p = (Phone) g.Info[1]; Console.WriteLine(p.Number); } } } } [C++] // 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"000-0000"; Phone* p2 = new Phone(); p2->Number = S"111-1111"; 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(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
XmlSerializer Class | XmlSerializer Members | System.Xml.Serialization Namespace