XmlElementAttribute Constructor (Type^)
Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. This type is used by the XmlSerializer when serializing or deserializing object that contains it.
Assembly: System.Xml (in System.Xml.dll)
Use the type parameter to specify a type that is derived from a base class. For example, suppose a property named MyAnimal returns an Animal object. You want to enhance the object, so you create a new class named Mammal that inherits from the Animal class. To instruct the XmlSerializer to accept the Mammal class when it serializes the MyAnimal property, pass the Type of the Mammal class to the constructor.
The following example serializes a class named Orchestra that contains a single field named Instruments, which returns an array of Instrument objects. A second class named Brass inherits from the Instrument class. The example applies the XmlElementAttribute to the Instruments field, and specifies the Brass type, allowing the Instruments field to accept Brass objects. The example also specifies the name of the XML element by setting the ElementName property.
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; public ref class Instrument { public: String^ Name; }; public ref class Brass: public Instrument { public: bool IsValved; }; public ref class Orchestra { public: array<Instrument^>^Instruments; }; void SerializeObject( String^ filename ) { // To write the file, a TextWriter is required. TextWriter^ writer = gcnew StreamWriter( filename ); XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); // Creates the object to serialize. Orchestra^ band = gcnew Orchestra; // Creates an object of the derived type. Brass^ i = gcnew Brass; i->Name = "Trumpet"; i->IsValved = true; array<Instrument^>^myInstruments = {i}; band->Instruments = myInstruments; s->Serialize( writer, band ); writer->Close(); } void DeserializeObject( String^ filename ) { XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); FileStream^ fs = gcnew FileStream( filename,FileMode::Open ); Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs )); Console::WriteLine( "Brass:" ); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass^ b; System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator(); while ( myEnum->MoveNext() ) { Instrument^ i = safe_cast<Instrument^>(myEnum->Current); b = dynamic_cast<Brass^>(i); Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved ); } } int main() { SerializeObject( "Override.xml" ); DeserializeObject( "Override.xml" ); }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1