Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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 Value

Type: System.Type
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();
   }
}
#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
{
public:
   String* Name;
};

public __gc class Manager: public Employee
{
public:
   int Level;
};

public __gc class Group
{
public:
   [XmlElement(__typeof(Manager))]
   Employee* Staff[];

public:
   [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();
   myGroup->ExtraInfo->Add(S".NET");
   myGroup->ExtraInfo->Add(__box(42));
   myGroup->ExtraInfo->Add(__box(DateTime(2001,1,1)));

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

int main()
{
   SerializeObject(S"TypeEx.xml");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.