XmlArrayItemAttribute Class
Assembly: System.Xml (in system.xml.dll)
'Declaration <AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=True)> _ Public Class XmlArrayItemAttribute Inherits Attribute 'Usage Dim instance As XmlArrayItemAttribute
/** @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.
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: |
|---|
| 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.
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
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
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.Reference
XmlArrayItemAttribute MembersSystem.Xml.Serialization Namespace
XmlArrayAttribute
XmlSerializer
XmlArrayItems
XmlAttributeOverrides
XmlAttributes
Other Resources
Introducing XML SerializationHow to: Specify an Alternate Element Name for an XML Stream
Controlling XML Serialization Using Attributes
Examples of XML Serialization
XML Schema Definition Tool (Xsd.exe)
Note: