XmlAttributes::XmlArray Property


Gets or sets an object that specifies how the XmlSerializer serializes a public field or read/write property that returns an array.

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

property XmlArrayAttribute^ XmlArray {
	XmlArrayAttribute^ get();
	void set(XmlArrayAttribute^ value);

Property Value

Type: System.Xml.Serialization::XmlArrayAttribute^

An XmlArrayAttribute that specifies how the XmlSerializer serializes a public field or read/write property that returns an array.

There are two ways in which a public field or public read/write property that returns an array is serialized by the XmlSerializer: the default serialization, and the controlled serialization.

In the default serialization, no attribute is applied to the member. When serialized, the array is serialized as a nested sequence of XML elements with the XML element name of the nested sequence taken from the member name.

To control the serialization more precisely, apply an XmlArrayAttribute to the field or property. For example, you can change the generated XML element name from the default to a different name by setting the ElementName property to a new value.

The XmlArray property allows you to override the default serialization, as well as the serialization controlled by applying an XmlArrayAttribute to the member. For example, you can change the XML element name generated from the default (the member identifier) to a new value. In addition, if you apply an XmlArrayAttribute to a member, it is overridden by any XmlArrayAttribute that is assigned to the XmlArray property.

The following example serializes a class that contains a field named Members that returns an array of objects. The XmlArrayAttribute is used to override the serialization of the field, and rename the element name to Staff.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Member
   String^ MemberName;

// This is the class that will be serialized.
public ref class Group

   // This field will be overridden.

// Return an XmlSerializer used for overriding. 
XmlSerializer^ CreateOverrider()
   // Creating XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAttrs = gcnew XmlAttributes;

   // Add an override for the XmlArray.    
   XmlArrayAttribute^ xArray = gcnew XmlArrayAttribute( "Staff" );
   xArray->Namespace = "http://www.cpandl.com";
   xAttrs->XmlArray = xArray;
   xOver->Add( Group::typeid, "Members", xAttrs );

   // Create the XmlSerializer and return it.
   return gcnew XmlSerializer( Group::typeid,xOver );

void SerializeObject( String^ filename )
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   Member^ m = gcnew Member;
   m->MemberName = "Paul";
   array<Member^>^temp = {m};
   myGroup->Members = temp;

   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize( writer, myGroup );

void DeserializeObject( String^ filename )
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   System::Collections::IEnumerator^ myEnum = myGroup->Members->GetEnumerator();
   while ( myEnum->MoveNext() )
      Member^ m = safe_cast<Member^>(myEnum->Current);
      Console::WriteLine( m->MemberName );

int main()
   SerializeObject( "OverrideArray.xml" );
   DeserializeObject( "OverrideArray.xml" );

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top