This documentation is archived and is not being maintained.

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)

'Declaration
Public Property XmlArray As XmlArrayAttribute
'Usage
Dim instance As XmlAttributes 
Dim value As XmlArrayAttribute 

value = instance.XmlArray

instance.XmlArray = 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.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized. 
Public Class Group
    ' This field will be overridden. 
    Public Members() As Member
End Class 

Public Class Member
    Public MemberName As String 
End Class 

Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideArray.xml")
        test.DeserializeObject("OverrideArray.xml")
    End Sub 

    ' Return an XmlSerializer used for overriding.  
    Public Function CreateOverrider() As XmlSerializer
        ' Creating XmlAttributeOverrides and XmlAttributes objects. 
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()

        ' Add an override for the XmlArray.     
        Dim xArray As New XmlArrayAttribute("Staff")
        xArray.Namespace = "http://www.cpandl.com"
        xAttrs.XmlArray = xArray
        xOver.Add(GetType(Group), "Members", xAttrs)

        ' Create the XmlSerializer and return it. 
        Return New XmlSerializer(GetType(Group), xOver)
    End Function 

    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class. 
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter. 
        Dim writer As New StreamWriter(filename)

        ' Create an instance of the class that will be serialized. 
        Dim myGroup As New Group()

        ' Set the object properties. 
        Dim m As New Member()
        m.MemberName = "Paul"
        myGroup.Members = New Member(0) {m}

        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub     

    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        Dim m As Member
        For Each m In  myGroup.Members
            Console.WriteLine(m.MemberName)
        Next m
    End Sub 
End Class
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Member
{
public:
   String* MemberName;
};

// This is the class that will be serialized.
public __gc class Group
{
   // This field will be overridden.
public:
   Member* Members[];
};

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

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

   // Create the XmlSerializer and return it.
   return new XmlSerializer(__typeof(Group), xOver);
}

void SerializeObject(String* filename)
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer* mySerializer =  CreateOverrider();
   // Writing the file requires a TextWriter.
   TextWriter* writer = new StreamWriter(filename);

   // Create an instance of the class that will be serialized.
   Group* myGroup = new Group();

   // Set the object properties.
   Member* m = new Member();
   m->MemberName = S"Paul";
   Member* temp[] = {m};
   myGroup->Members = temp;

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

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

int main()
{
   SerializeObject(S"OverrideArray.xml");
   DeserializeObject(S"OverrideArray.xml");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: