XmlAttributes.XmlArrayItems Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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 XmlArrayItemAttributes XmlArrayItems { get; }

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.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

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

public class Member
    public string MemberName;

public class NewMember:Member
    public int MemberID;

public class RetiredMember:NewMember
    public DateTime RetireDate;

public class Run
   public static void Main()
      Run test = new Run();

   // 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(typeof(NewMember));
      xArrayItem.Namespace = "http://www.cpandl.com";

      // Add a second override.
      XmlArrayItemAttribute xArrayItem2 = 
      new XmlArrayItemAttribute(typeof(RetiredMember));
      xArrayItem2.Namespace = "http://www.cpandl.com";

      // Add all overrides to XmlAttribueOverrides object.
      xOver.Add(typeof(Group), "Members", xAttrs);

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

   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[2] {m, m2};

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

   public void DeserializeObject(string filename)
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      foreach(Member m in myGroup.Members)

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