XmlArrayItemAttribute Class
Specifies the derived types that the XmlSerializer can place in a serialized array.
For a list of all members of this type, see XmlArrayItemAttribute Members.
System.Object
System.Attribute
System.Xml.Serialization.XmlArrayItemAttribute
[Visual Basic] <AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _ Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)> Public Class XmlArrayItemAttribute Inherits Attribute [C#] [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue)] public class XmlArrayItemAttribute : Attribute [C++] [AttributeUsage(AttributeTargets::Property | AttributeTargets::Field | AttributeTargets::Parameter | AttributeTargets::ReturnValue)] public __gc class XmlArrayItemAttribute : public Attribute [JScript] public AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue) class XmlArrayItemAttribute extends Attribute
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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.
Note You can apply multiple instances of the XmlArrayItemAttribute or XmlElementAttribute to specify types of objects that can be inserted into the array.
Note 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.
Note In your code, you can use the word XmlArrayItem instead of the longer XmlArrayItemAttribute.
Example
[Visual Basic, C#, C++] The following example serializes a class named Group, which 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.
[Visual Basic] Option Explicit Option Strict Imports System Imports System.IO Imports System.Xml.Serialization Imports Microsoft.VisualBasic Public Class Group ' The XmlArrayItemAttribute allows the XmlSerializer to insert ' both the base type (Employee) and derived type (Manager) ' into serialized arrays. <XmlArrayItem(GetType(Manager)), _ XmlArrayItem(GetType(Employee))> _ Public Employees() As Employee ' Use the XmlArrayItemAttribute to specify types allowed ' in an array of Object items. <XmlArray(), _ XmlArrayItem(GetType(Integer), ElementName := "MyNumber"), _ XmlArrayItem(GetType(String), ElementName := "MyString"), _ XmlArrayItem(GetType(Manager))> _ Public ExtraInfo() As Object End Class Public Class Employee Public Name As String End Class Public Class Manager Inherits Employee Public Level As Integer End Class Public Class Run Public Shared Sub Main() Dim test As New Run() test.SerializeObject("TypeDoc.xml") test.DeserializeObject("TypeDoc.xml") End Sub Public Sub SerializeObject(ByVal filename As String) ' Creates a new XmlSerializer. Dim s As New XmlSerializer(GetType(Group)) ' Writing the XML file to disk requires a TextWriter. Dim writer As New StreamWriter(filename) Dim group As New Group() Dim manager As New Manager() Dim emp1 As New Employee() Dim emp2 As New Employee() manager.Name = "Consuela" manager.Level = 3 emp1.Name = "Seiko" emp2.Name = "Martina" Dim emps() As Employee = {manager, emp1, emp2} group.Employees = emps ' Creates an int and a string and assigns to ExtraInfo. group.ExtraInfo = New Object() {43, "Extra", manager} ' Serializes the object, and closes the StreamWriter. s.Serialize(writer, group) writer.Close() End Sub Public Sub DeserializeObject(ByVal filename As String) Dim fs As New FileStream(filename, FileMode.Open) Dim x As New XmlSerializer(GetType(Group)) Dim g As Group = CType(x.Deserialize(fs), Group) Console.WriteLine("Members:") Dim e As Employee For Each e In g.Employees Console.WriteLine(ControlChars.Tab & e.Name) Next e End Sub End Class [C#] 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); } } } [C++] #using <mscorlib.dll> #using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; public __gc class Employee { public: String* Name; }; public __gc class Manager:public Employee { public: int Level; }; public __gc class Group { /* The XmlArrayItemAttribute allows the XmlSerializer to insert both the base type (Employee) and derived type (Manager) into serialized arrays. */ public: [XmlArrayItem(__typeof(Manager)), XmlArrayItem(__typeof(Employee))] Employee* Employees[]; /* Use the XmlArrayItemAttribute to specify types allowed in an array of Object items. */ [XmlArray] [XmlArrayItem (__typeof(Int32), ElementName = S"MyNumber"), XmlArrayItem (__typeof(String), ElementName = S"MyString"), XmlArrayItem(__typeof(Manager))] Object* ExtraInfo[]; }; 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 = S"Consuela"; manager->Level = 3; emp1->Name = S"Seiko"; emp2->Name = S"Martina"; Employee* emps[] = {manager, emp1, emp2}; group->Employees = emps; // Creates an int and a string and assigns to ExtraInfo. Object* temp[] = {__box(43), S"Extra", manager}; group->ExtraInfo = temp; // Serializes the object, and closes the StreamWriter. s->Serialize(writer, group); writer->Close(); } void DeserializeObject(String* filename) { FileStream* fs = new FileStream(filename, FileMode::Open); XmlSerializer* x = new XmlSerializer(__typeof(Group)); Group* g = dynamic_cast<Group*> (x->Deserialize(fs)); Console::WriteLine(S"Members:"); System::Collections::IEnumerator* myEnum = g->Employees->GetEnumerator(); while (myEnum->MoveNext()) { Employee* e = __try_cast<Employee*>(myEnum->Current); Console::WriteLine(S"\t{0}", e->Name); } } int main() { SerializeObject(S"TypeDoc.xml"); DeserializeObject(S"TypeDoc.xml"); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Xml.Serialization
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Xml (in System.Xml.dll)
See Also
XmlArrayItemAttribute Members | System.Xml.Serialization Namespace | XmlArrayAttribute | XmlSerializer | XmlArrayItems | XmlAttributeOverrides | XmlAttributes | Introducing XML Serialization | Overriding XML Serialization | XmlAttributes | Controlling XML Serialization Using Attributes | Examples of XML Serialization