This documentation is archived and is not being maintained.

XmlElementAttribute.Type Property

Gets or sets the object type used to represent the XML element.

[Visual Basic]
Public Property Type As Type
public Type Type {get; set;}
public: __property Type* get_Type();
public: __property void set_Type(Type*);
public function get Type() : Type;
public function set Type(Type);

Property Value

The Type of the member.


Use the Type property to specify a derived type for a field or property.

If a field or property returns an ArrayList, you can apply multiple instances of the XmlElementAttribute to the member. For each instance, set the Type property to a type of object that can be inserted into the array.


[Visual Basic, C#, C++] The following example uses the Type property to specify a derived object for an XmlElementAttribute. The example also applies three instances of the XmlElementAttribute to a field that returns an ArrayList. Each instance specifies a type allowed in the field.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Group
    <XmlElement(GetType(Manager))> _    
    Public Staff() As Employee
    <XmlElement(GetType(Integer)), _
     XmlElement(GetType(String)), _
     XmlElement(GetType(DateTime))> _
    Public ExtraInfo As ArrayList
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()
    End Sub    
    Public Sub SerializeObject(filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As New XmlSerializer(GetType(Group))
        ' Create an object and serialize it.
        Dim myGroup As New Group()
        Dim e1 As New Manager()
        e1.Name = "Manager1"
        Dim m1 As New Manager()
        m1.Name = "Manager2"
        m1.Level = 4
        Dim emps() As Employee = {e1, m1}
        myGroup.Staff = emps
        myGroup.ExtraInfo = New ArrayList()
        myGroup.ExtraInfo.Add(New DateTime(2001, 1, 1))
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
    End Sub
End Class

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

public class Group
   public Employee [] Staff;

   [XmlElement (typeof(int)),
   XmlElement (typeof(string)),
   XmlElement (typeof(DateTime))]
   public ArrayList 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();

   public void SerializeObject(string filename)
      // Create an XmlSerializer instance.
      XmlSerializer xSer = 
      new XmlSerializer(typeof(Group));

      // Create object and serialize it.
      Group myGroup = new Group();
      Manager e1 = new Manager();
      e1.Name = "Manager1";
      Manager m1 =  new Manager();
      m1.Name = "Manager2";
      m1.Level = 4;

      Employee[] emps = {e1, m1};
      myGroup.Staff = emps;

      myGroup.ExtraInfo = new ArrayList();
      myGroup.ExtraInfo.Add(new DateTime(2001,1,1));
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Employee
   String* Name;

public __gc class Manager: public Employee
   int Level;

public __gc class Group
   Employee* Staff[];

   [XmlElement (__typeof(Int32)),
      XmlElement (__typeof(String)),
      XmlElement (__typeof(DateTime))]
   ArrayList* ExtraInfo;

void SerializeObject(String* filename)
   // Create an XmlSerializer instance.
   XmlSerializer* xSer = 
      new XmlSerializer(__typeof(Group));

   // Create object and serialize it.
   Group* myGroup = new Group();

   Manager* e1 = new Manager();
   e1->Name = S"Manager1";
   Manager* m1 =  new Manager();
   m1->Name = S"Manager2";
   m1->Level = 4;

   Employee* emps[] = {e1, m1};
   myGroup->Staff = emps;

   myGroup->ExtraInfo = new ArrayList();

   TextWriter* writer = new StreamWriter(filename);
   xSer->Serialize(writer, myGroup);

int main()

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


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

See Also

XmlElementAttribute Class | XmlElementAttribute Members | System.Xml.Serialization Namespace