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)

public:
property XmlArrayItemAttributes^ XmlArrayItems {
	XmlArrayItemAttributes^ get ();
}
/** @property */
public XmlArrayItemAttributes get_XmlArrayItems ()

public function get XmlArrayItems () : XmlArrayItemAttributes

Not applicable.

Property Value

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.

#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
{
public:
   String^ MemberName;
};

// This is the class that will be serialized.
public ref class Group
{
public:
   array<Member^>^Members;
};

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

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

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

   // Add an override for the XmlArrayItem.    
   XmlArrayItemAttribute^ xArrayItem = gcnew XmlArrayItemAttribute( NewMember::typeid );
   xArrayItem->Namespace = "http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add( xArrayItem );

   // Add a second override.
   XmlArrayItemAttribute^ xArrayItem2 = gcnew XmlArrayItemAttribute( RetiredMember::typeid );
   xArrayItem2->Namespace = "http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add( xArrayItem2 );

   // Add all overrides to XmlAttribueOverrides object.
   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.
   NewMember^ m = gcnew NewMember;
   m->MemberName = "Paul";
   m->MemberID = 2;

   // Create a second member.
   RetiredMember^ m2 = gcnew RetiredMember;
   m2->MemberName = "Renaldo";
   m2->MemberID = 23;
   m2->RetireDate = DateTime(2000,10,10);
   array<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 = 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( "OverrideArrayItem.xml" );
   DeserializeObject( "OverrideArrayItem.xml" );
}

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// This is the class that will be serialized.
public class Group
{
    public Member members[];
} //Group

public class Member
{
    public String memberName;
} //Member

public class NewMember extends Member
{
    public int memberID;
} //NewMember

public class RetiredMember extends NewMember
{
    public DateTime retireDate;
} //RetiredMember

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideArrayItem.xml");
        test.DeserializeObject("OverrideArrayItem.xml");
    } //main

    // Return an XmlSerializer used for overriding. 
    public 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(NewMember.class.ToType());

        xArrayItem.set_Namespace("http://www.cpandl.com");
        xAttrs.get_XmlArrayItems().Add(xArrayItem);

        // Add a second override.
        XmlArrayItemAttribute xArrayItem2 =
            new XmlArrayItemAttribute(RetiredMember.class.ToType());

        xArrayItem2.set_Namespace("http://www.cpandl.com");
        xAttrs.get_XmlArrayItems().Add(xArrayItem2);

        // Add all overrides to XmlAttribueOverrides object.
        xOver.Add(Group.class.ToType(), "members", xAttrs);

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

    public 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 = "Paul";
        m.memberID = 2;

        // Create a second member.
        RetiredMember m2 = new RetiredMember();
        m2.memberName = "Renaldo";
        m2.memberID = 23;
        m2.retireDate = new DateTime(2000, 10, 10);
        myGroup.members = new Member[] { m, m2 };

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

    public void DeserializeObject(String fileName)
    {
        XmlSerializer mySerializer = CreateOverrider();
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup = (Group)mySerializer.Deserialize(fs);

        for (int iCtr = 0; iCtr < myGroup.members.length; iCtr++) {
            Member m = myGroup.members[iCtr];
            Console.WriteLine(m.memberName);
        }
    } //DeserializeObject
} //Run

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft