Export (0) Print
Expand All

XmlElementAttribute.Type Property

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

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

public Type Type { get; set; }
/** @property */
public Type get_Type ()

/** @property */
public void set_Type (Type value)

public function get Type () : Type

public function set Type (value : Type)

Not applicable.

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.

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.

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

public class Group
{
   [XmlElement(typeof(Manager))]
   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();
      test.SerializeObject("TypeEx.xml");
   }

   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(".NET");
      myGroup.ExtraInfo.Add(42);
      myGroup.ExtraInfo.Add(new DateTime(2001,1,1));
      
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}


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

public class Group
{
    /** @attribute XmlElement(Manager.class)
     */
    public Employee staff[];

    /** @attribute XmlElement(int.class)
     *  @attribute XmlElement(String.class)
     *  @attribute XmlElement(DateTime.class)
     */
    public ArrayList 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("TypeEx.xml");
    } //main

    public void SerializeObject(String fileName)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType());

        // 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(".NET");
        myGroup.extraInfo.Add((Int32)42);
        myGroup.extraInfo.Add(new DateTime(2001, 1, 1));

        TextWriter writer = new StreamWriter(fileName);
        xSer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject
} //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.

.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