XmlAttributes Class
Represents a collection of attribute objects that control how the XmlSerializer serializes and deserializes an object.
Assembly: System.Xml (in System.Xml.dll)
The XmlAttributes type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | XmlAttributes() | Initializes a new instance of the XmlAttributes class. |
![]() ![]() | XmlAttributes(ICustomAttributeProvider) | Infrastructure. Initializes a new instance of the XmlAttributes class and customizes how the XmlSerializer serializes and deserializes an object. |
| Name | Description | |
|---|---|---|
![]() ![]() | XmlAnyAttribute | Gets or sets the XmlAnyAttributeAttribute to override. |
![]() ![]() ![]() | XmlAnyElements | Gets the collection of XmlAnyElementAttribute objects to override. |
![]() ![]() ![]() | XmlArray | Gets or sets an object that specifies how the XmlSerializer serializes a public field or read/write property that returns an array. |
![]() ![]() ![]() | XmlArrayItems | Gets or sets a collection of objects that specify how the XmlSerializer serializes items inserted into an array returned by a public field or read/write property. |
![]() ![]() ![]() | XmlAttribute | Gets or sets an object that specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute. |
![]() ![]() ![]() | XmlChoiceIdentifier | Gets or sets an object that allows you to distinguish between a set of choices. |
![]() ![]() ![]() | XmlDefaultValue | Gets or sets the default value of an XML element or attribute. |
![]() ![]() ![]() | XmlElements | Gets a collection of objects that specify how the XmlSerializer serializes a public field or read/write property as an XML element. |
![]() ![]() ![]() | XmlEnum | Gets or sets an object that specifies how the XmlSerializer serializes an enumeration member. |
![]() ![]() ![]() | XmlIgnore | Gets or sets a value that specifies whether or not the XmlSerializer serializes a public field or public read/write property. |
![]() ![]() ![]() | Xmlns | Gets or sets a value that specifies whether to keep all namespace declarations when an object containing a member that returns an XmlSerializerNamespaces object is overridden. |
![]() ![]() ![]() | XmlRoot | Gets or sets an object that specifies how the XmlSerializer serializes a class as an XML root element. |
![]() ![]() ![]() | XmlText | Gets or sets an object that instructs the XmlSerializer to serialize a public field or public read/write property as XML text. |
![]() ![]() ![]() | XmlType | Gets or sets an object that specifies how the XmlSerializer serializes a class to which the XmlTypeAttribute has been applied. |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
Creating the XmlAttributes is part of a process that overrides the default way the XmlSerializer serializes class instances. For example, suppose you want to serialize an object that is created from a DLL which has an inaccessible source. By using the XmlAttributeOverrides, you can augment or otherwise control how the object is serialized.
The members of the XmlAttributes class correspond directly to a family of attribute classes that control serialization. For example, the XmlText property must be set to an XmlTextAttribute, which allows you to override serialization of a field or property by instructing the XmlSerializer to serialize the property value as XML text. For a complete list of attributes that control serialization, see the XmlSerializer.
For more details on using the XmlAttributeOverrides with the XmlAttributes class, see How to: Specify an Alternate Element Name for an XML Stream.
The following example serializes an instance of a class named Orchestra, which contains a single field named Instruments that returns an array of Instrument objects. A second class named Brass inherits from the Instrument class. The example creates an XmlAttributes object to override the Instrument field--allowing the field to accept Brass objects--and adds the XmlAttributes object to an instance of the XmlAttributeOverrides class.
#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 ) { /* Each overridden field, property, or type requires an XmlAttributes object. */ XmlAttributes^ attrs = gcnew XmlAttributes; /* Create an XmlElementAttribute to override the field that returns Instrument objects. The overridden field returns Brass objects instead. */ XmlElementAttribute^ attr = gcnew XmlElementAttribute; attr->ElementName = "Brass"; attr->Type = Brass::typeid; // Add the element to the collection of elements. attrs->XmlElements->Add( attr ); // Create the XmlAttributeOverrides object. XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; /* Add the type of the class that contains the overridden member and the XmlAttributes to override it with to the XmlAttributeOverrides object. */ attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Create the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); // Writing the file requires a TextWriter. TextWriter^ writer = gcnew StreamWriter( filename ); // Create the object that will be serialized. Orchestra^ band = gcnew Orchestra; // Create an object of the derived type. Brass^ i = gcnew Brass; i->Name = "Trumpet"; i->IsValved = true; array<Instrument^>^myInstruments = {i}; band->Instruments = myInstruments; // Serialize the object. s->Serialize( writer, band ); writer->Close(); } void DeserializeObject( String^ filename ) { XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Create an XmlElementAttribute to override the Instrument. XmlElementAttribute^ attr = gcnew XmlElementAttribute; attr->ElementName = "Brass"; attr->Type = Brass::typeid; // Add the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Create 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:" ); /* The difference between deserializing the overridden XML document and serializing it is this: To read the derived object values, you must 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" ); }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

