Export (0) Print
Expand All

XmlAttributes.XmlArrayItems Property

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.

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

'Declaration
Public ReadOnly Property XmlArrayItems As XmlArrayItemAttributes
'Usage
Dim instance As XmlAttributes 
Dim value As XmlArrayItemAttributes 

value = instance.XmlArrayItems

Property Value

Type: System.Xml.Serialization.XmlArrayItemAttributes
An XmlArrayItemAttributes object that contains a collection of XmlArrayItemAttribute objects.

The XmlArrayItems property allows you to specify the derived types that can be inserted into an array returned by a public field or public read/write property. For each new type you want the field or property to accept, create an XmlArrayItemAttribute object and Add it to the XmlArrayItemAttributes) returned by the XmlArrayItems property. (The new type must be derived from the type already accepted by the field or property.) Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property, and the name of the field or property. Construct an XmlSerializer with the XmlAttributeOverrides object before calling Serialize or Deserialize method.

The following example serializes a class that contains a field named Members that returns an array of objects. Two XmlArrayItemAttribute objects are created to allow the field to accept objects that derive from the base class named Member. Each object is added to the XmlAttributes through the XmlArrayItems property.

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


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

Public Class Member
    Public MemberName As String 
End Class 

Public Class NewMember
    Inherits Member
    Public MemberID As Integer 
End Class 

Public Class RetiredMember
    Inherits NewMember
    Public RetireDate As DateTime
End Class 


Public Class Run

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

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

        ' Add an override for the XmlArrayItem.     
        Dim xArrayItem As New XmlArrayItemAttribute(GetType(NewMember))
        xArrayItem.Namespace = "http://www.cpandl.com"
        xAttrs.XmlArrayItems.Add(xArrayItem)

        ' Add a second override. 
        Dim xArrayItem2 As New XmlArrayItemAttribute(GetType(RetiredMember))
        xArrayItem2.Namespace = "http://www.cpandl.com"
        xAttrs.XmlArrayItems.Add(xArrayItem2)

        ' Add all overrides to XmlAttribueOverrides object.
        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 NewMember()
        m.MemberName = "Paul"
        m.MemberID = 2

        ' Create a second member. 
        Dim m2 As New RetiredMember()
        m2.MemberName = "Renaldo"
        m2.MemberID = 23
        m2.RetireDate = New DateTime(2000, 10, 10)

        myGroup.Members = New Member(1) {m, m2}

        ' 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
{
public:
   Member* Members[];
};

public __gc class NewMember:public Member
{
public:
   int MemberID;
};

public __gc class RetiredMember:public NewMember
{
public:
   DateTime RetireDate;
};

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

   // Add an override for the XmlArrayItem.    
   XmlArrayItemAttribute* xArrayItem = 
      new XmlArrayItemAttribute(__typeof(NewMember));
   xArrayItem->Namespace = S"http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add(xArrayItem);

   // Add a second override.
   XmlArrayItemAttribute* xArrayItem2 = 
      new XmlArrayItemAttribute(__typeof(RetiredMember));
   xArrayItem2->Namespace = S"http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add(xArrayItem2);

   // Add all overrides to XmlAttribueOverrides object.
   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.
   NewMember* m = new NewMember();
   m->MemberName = S"Paul";
   m->MemberID = 2;

   // Create a second member.
   RetiredMember* m2 = new RetiredMember();
   m2->MemberName = S"Renaldo";
   m2->MemberID = 23;
   m2->RetireDate = DateTime(2000, 10,10);

   Member* temp[] = {m, m2};
   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"OverrideArrayItem.xml");
   DeserializeObject(S"OverrideArrayItem.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

Community Additions

ADD
Show:
© 2014 Microsoft