Export (0) Print
Expand All

XmlArrayItemAttribute Class

Specifies the derived types that the XmlSerializer can place in a serialized array.

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

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true)] 
public class XmlArrayItemAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true) */ 
public class XmlArrayItemAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true) 
public class XmlArrayItemAttribute extends Attribute
Not applicable.

The XmlArrayItemAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.

You can apply the XmlArrayItemAttribute to any public read/write member that returns an array, or provides access to one. For example, a field that returns an array of objects, a collection, an ArrayList, or any class that implements the IEnumerable interface.

The XmlArrayItemAttribute supports polymorphism--in other words, it allows the XmlSerializer to add derived objects to an array. For example, suppose a class named Mammal is derived from a base class named Animal. Further suppose that a class named MyAnimals contains a field that returns an array of Animal objects. To allow the XmlSerializer to serialize both the Animal and Mammal type, apply the XmlArrayItemAttribute to the field twice, each time specifying one of the two acceptable types.

NoteNote:

You can apply multiple instances of the XmlArrayItemAttribute or XmlElementAttribute to specify types of objects that can be inserted into the array.

NoteNote:

The serialization of a field or property that returns an interface or array of interfaces is not supported.

For more information about using attributes, see Extending Metadata Using Attributes.

NoteNote:

You can use the word XmlArrayItem in your code instead of the longer XmlArrayItemAttribute.

The following example serializes a class named Group that contains a field named Employees that returns an array of Employee objects. The example applies the XmlArrayItemAttribute to the field, thereby instructing the XmlSerializer that it can insert objects of both the base class (Employee) type and derived class type (Manager) into the serialized array.

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

public class Group
{  
   /* The XmlArrayItemAttribute allows the XmlSerializer to insert
      both the base type (Employee) and derived type (Manager) 
      into serialized arrays. */

   [XmlArrayItem(typeof(Manager)),
   XmlArrayItem(typeof(Employee))]
   public Employee[] Employees;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (typeof(int),
   ElementName = "MyNumber"),
   XmlArrayItem (typeof(string),
   ElementName = "MyString"),
   XmlArrayItem(typeof(Manager))]
   public object [] ExtraInfo;
}   

public class Employee
{
   public string Name;
}

public class Manager:Employee{
   public int Level;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("TypeDoc.xml");
      test.DeserializeObject("TypeDoc.xml");
   }


   public void SerializeObject(string filename)
   {
      // Creates a new XmlSerializer.
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);
      Group group = new Group();
      
      Manager manager = new Manager();
      Employee emp1 = new Employee();
      Employee emp2 = new Employee();
      manager.Name = "Consuela";
      manager.Level = 3;
      emp1.Name = "Seiko";
      emp2.Name = "Martina";
      Employee [] emps = new Employee[3]{manager, emp1, emp2};
      group.Employees = emps;

      // Creates an int and a string and assigns to ExtraInfo.
      group.ExtraInfo = new Object[3]{43, "Extra", manager};

      // Serializes the object, and closes the StreamWriter.
      s.Serialize(writer, group);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      FileStream fs = new FileStream(filename, FileMode.Open);
      XmlSerializer x = new XmlSerializer(typeof(Group));
      Group g = (Group) x.Deserialize(fs);
      Console.WriteLine("Members:");
      
      foreach(Employee e in g.Employees) 
      {
         Console.WriteLine("\t" + e.Name);
      }
   }
}
   

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

public class Group
{
    /* The XmlArrayItemAttribute allows the XmlSerializer to insert
       both the base type (Employee) and derived type (Manager) 
       into serialized arrays. 
    */
    /** @attribute XmlArrayItem(Manager.class)
        @attribute XmlArrayItem(Employee.class)
     */
    public Employee employees[];

    /* Use the XmlArrayItemAttribute to specify types allowed
       in an array of Object items. 
    */
    /** @attribute XmlArray()
     */
    /** @attribute XmlArrayItem(int.class, ElementName = "MyNumber")
        @attribute XmlArrayItem(String.class, ElementName = "MyString")
        @attribute XmlArrayItem(Manager.class)
     */
    public Object extraInfo[];
} //Group

public class Employee
{
    public String name;
} //Employee

public class Manager extends Employee
{
    public int level;
} //Manager

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

    public void SerializeObject(String fileName)
    {
        // Creates a new XmlSerializer.
        XmlSerializer s = new XmlSerializer(Group.class.ToType());

        // Writing the XML file to disk requires a TextWriter.
        TextWriter writer = new StreamWriter(fileName);
        Group group = new Group();
        Manager manager = new Manager();
        Employee emp1 = new Employee();
        Employee emp2 = new Employee();

        manager.name = "Consuela";
        manager.level = 3;
        emp1.name = "Seiko";
        emp2.name = "Martina";

        Employee emps[] = new Employee[] { manager, emp1, emp2 };
        group.employees = emps;

        // Creates an int and a string and assigns to ExtraInfo.
        group.extraInfo = new Object[] { (Int32)43, "Extra", manager };

        // Serializes the object, and closes the StreamWriter.
        s.Serialize(writer, group);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        FileStream fs = new FileStream(fileName, FileMode.Open);
        XmlSerializer x = new XmlSerializer(Group.class.ToType());
        Group g = (Group)(x.Deserialize(fs));

        Console.WriteLine("Members:");
        for (int iCtr = 0; iCtr < g.employees.length; iCtr++) {
            Employee e = (Employee)g.employees.get_Item(iCtr);
            Console.WriteLine("\t" + e.name);
        }
    } //DeserializeObject
} //Run

System.Object
   System.Attribute
    System.Xml.Serialization.XmlArrayItemAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft