This documentation is archived and is not being maintained.

XmlElementAttribute Constructor (Type)

Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. This type is used by the XmlSerializer when serializing or deserializing object that contains it.

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

public XmlElementAttribute (
	Type type
)
public XmlElementAttribute (
	Type type
)
public function XmlElementAttribute (
	type : Type
)

Parameters

type

The Type of an object derived from the member's type.

Use the type parameter to specify a type that is derived from a base class. For example, suppose a property named MyAnimal returns an Animal object. You want to enhance the object, so you create a new class named Mammal that inherits from the Animal class. To instruct the XmlSerializer to accept the Mammal class when it serializes the MyAnimal property, pass the Type of the Mammal class to the constructor.

The following example serializes a class named Orchestra that contains a single field named Instruments, which returns an array of Instrument objects. A second class named Brass inherits from the Instrument class. The example applies the XmlElementAttribute to the Instruments field, and specifies the Brass type, allowing the Instruments field to accept Brass objects. The example also specifies the name of the XML element by setting the ElementName property.

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

public class Orchestra
{
   public Instrument[] Instruments;
}   

public class Instrument
{
   public string Name;
}

public class Brass:Instrument{
   public bool IsValved;
}

public class Run
{
    public static void Main()
    {
       Run test = new Run();
       test.SerializeObject("Override.xml");
       test.DeserializeObject("Override.xml");
    }

    public void SerializeObject(string filename)
    {
      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      
      XmlAttributeOverrides attrOverrides = 
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that overrides the Instrument type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s = 
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      // Creates the object to serialize.
      Orchestra band = new Orchestra();
      
      // Creates an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides = 
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that override the Instrument type.
      XmlElementAttribute attr = new 
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s = 
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* Deserializing differs from serializing. To read the 
         derived-object values, declare an object of the derived 
         type (Brass) and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments) 
      {
         b= (Brass)i;
         Console.WriteLine(
         b.Name + "\n" + 
         b.IsValved);
      }
   }
}


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

public class Orchestra
{
    public Instrument instruments[];
} //Orchestra

public class Instrument
{
    public String name;
} //Instrument

public class Brass extends Instrument
{
    public boolean isValved;
} //Brass

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("Override.xml");
        test.DeserializeObject("Override.xml");
    } //main

    public void SerializeObject(String fileName)
    {
        // To write the file, a TextWriter is required.
        TextWriter writer = new StreamWriter(fileName);
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that overrides the Instrument type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(), attrOverrides);

        // Creates the object to serialize.
        Orchestra band = new Orchestra();

        // Creates an object of the derived type.
        Brass i = new Brass();
        i.name = "Trumpet";
        i.isValved = true;
        Instrument myInstruments[] = { i };
        band.instruments = myInstruments;
        s.Serialize(writer, band);
        writer.Close();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute that override the Instrument type.
        XmlElementAttribute attr =
            new XmlElementAttribute(Brass.class.ToType());
        attr.set_ElementName("Brass");

        // Adds the element to the collection of elements.
        attrs.get_XmlElements().Add(attr);
        attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(), attrOverrides);
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Orchestra band = (Orchestra)s.Deserialize(fs);

        Console.WriteLine("Brass:");

        /* Deserializing differs from serializing. To read the 
           derived-object values, declare an object of the derived 
           type (Brass) and cast the Instrument instance to it. */

        Brass b;
        for (int iCtr = 0; iCtr < band.instruments.length; iCtr++) {
            Instrument i = band.instruments[iCtr];
            b = (Brass)i;
            Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved));
        }
    } //DeserializeObject
} //Run

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: