XmlSerializer Constructor (Type, XmlAttributeOverrides)
.NET Framework 2.0
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)
XmlSerializer Members
System.Xml.Serialization Namespace
XmlAttributeOverrides Class
XmlAttributes Class
How to: Specify an Alternate Element Name for an XML Stream
Controlling XML Serialization Using Attributes
Examples of XML Serialization
XML Schema Definition Tool (Xsd.exe)
Assembly: System.Xml (in system.xml.dll)
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"); } } } }
package College;
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
import HighSchool.*;
public class Graduate extends HighSchool.Student
{
public Graduate()
{
} //Graduate
// Add a new field named University.
public String university;
} //Graduate
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.WriteOverriddenAttributes("College.xml");
test.ReadOverriddenAttributes("College.xml");
} //main
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", Graduate.class.ToType());
/* Add the XmlElementAttribute to the collection of
elements in the XmlAttributes object. */
attrs.get_XmlElements().Add(attr);
/* Add the XmlAttributes to the XmlAttributeOverrides.
"Students" is the name being overridden. */
attrOverrides.Add(HighSchool.MyClass.class.ToType(), "students", attrs);
// Create the XmlSerializer.
XmlSerializer mySerializer =
new XmlSerializer(HighSchool.MyClass.class.ToType(), 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();
} //WriteOverriddenAttributes
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", Graduate.class.ToType());
attrs.get_XmlElements().Add(attr);
attrOverrides.Add(HighSchool.MyClass.class.ToType(), "students", attrs);
XmlSerializer readSerializer =
new XmlSerializer(HighSchool.MyClass.class.ToType(), 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;
for (int iCtr = 0; iCtr < myClass.students.length; iCtr++) {
Graduate grad = (Graduate)myClass.students[iCtr];
g = (Graduate)grad;
Console.Write(g.name + "\t");
Console.Write(g.iD + "\t");
Console.Write(g.university + "\n");
}
} //ReadOverriddenAttributes
} //Run
package HighSchool;
// Beginning of HighSchool.dll
public class Student
{
public String name;
public int iD;
} //Student
public class MyClass
{
public Student students[];
} //MyClass
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.
Reference
XmlSerializer ClassXmlSerializer Members
System.Xml.Serialization Namespace
XmlAttributeOverrides Class
XmlAttributes Class
Other Resources
Introducing XML SerializationHow to: Specify an Alternate Element Name for an XML Stream
Controlling XML Serialization Using Attributes
Examples of XML Serialization
XML Schema Definition Tool (Xsd.exe)