System.Xml.Serialization.XmlAttributeOverrides Class

Assembly: System.Xml.dll
Namespace: System.Xml.Serialization
Summary
Allows you to override property, field, and class attributes when you use the XmlSerializer to serialize or deserialize an object.
C# Syntax:
public class XmlAttributeOverrides
Remarks
The XmlAttributeOverrides enables the XmlSerializer to override the default way of serializing a set of objects. Overriding serialization in this way has two uses: first, you can control and augment the serialization of objects found in a DLL--even if you don't have access to the source; second, you can create one set of serializable classes, but serialize the objects in multiple ways. For example, instead of serializing members of a class instance as XML elements, you can serialize them as XML attributes, resulting in a more efficient document to transport.

After you create an XmlAttributeOverrides object, you pass it as an argument to the XmlSerializer.#ctor constructor. The resulting XmlSerializer uses the data contained by the XmlAttributeOverrides to override attributes that control how objects are serialized. To accomplish this goal, the XmlAttributeOverrides contains a collection of the object types that will be overridden, as well as an XmlAttributes object associated with each overridden object type. The XmlAttributes object itself contains an appropriate set of attribute objects that control how each field, property, or class is serialized.

The process for creating and using an XmlAttributeOverrides object is as follows:

  1. Create an XmlAttributes object.
  2. Create an attribute object that is appropriate to the object being overridden. For example, to override a field or property, create an XmlElementAttribute, using the new, derived type. You can optionally assign a new XmlElementAttribute.ElementName, or XmlElementAttribute.Namespace that will override the base class's attribute name or namespace.
  3. Add the attribute object to the appropriate XmlAttributes property or collection. For example, you would add the XmlElementAttribute to the XmlAttributes.XmlElements collection of the XmlAttributes object, specifying the member name that is being overridden.
  4. Create an XmlAttributeOverrides object.
  5. Using the XmlAttributeOverrides.Add method, add the XmlAttributes object to the XmlAttributeOverrides object. If the object being overridden is an XmlRootAttribute or XmlTypeAttribute, you need only specify the type of the overridden object. But if you are overridding a field or property, you must also specify the name of the overridden member.
  6. When constructing the XmlSerializer, pass the XmlAttributeOverrides to the XmlSerializer.#ctor constructor.
  7. Use the resulting XmlSerializer to serialize or deserialize the derived class objects.
Example
The following example serializes a class named Orchestra, which contains a single field named Instruments that returns an array of Instrument objects. A second class named Brass inherits from the Instrument class. The example uses an instance of the XmlAttributeOverrides class to override the Instrument field, allowing the field to accept Brass objects.
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)
    {
      /* Each overridden field, property, or type requires 
      an XmlAttributes object. */
      XmlAttributes attrs = new XmlAttributes();

      /* Create an XmlElementAttribute to override the 
      field that returns Instrument objects. The overridden field
      returns Brass objects instead. */
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the element to the collection of elements.
      attrs.XmlElements.Add(attr);

      // Create the XmlAttributeOverrides object.
      XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

      /* Add the type of the class that contains the overridden 
      member and the XmlAttributes to override it with to the 
      XmlAttributeOverrides object. */
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

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

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create the object that will be serialized.
      Orchestra band = new Orchestra();
      
      // Create an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;

      // Serialize the object.
      s.Serialize(writer,band);
      writer.Close();
   }

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

      // Create an XmlElementAttribute to override the Instrument.
      XmlElementAttribute attr = new XmlElementAttribute();
      attr.ElementName = "Brass";
      attr.Type = typeof(Brass);

      // Add the XmlElementAttribute to the collection of objects.
      attrs.XmlElements.Add(attr);

      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Create 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:");

      /* The difference between deserializing the overridden 
      XML document and serializing it is this: To read the derived 
      object values, you must 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);
      }
   }
}


    
See also:
System.Xml.Serialization Namespace | XmlSerializer.Deserialize | XmlSerializer.Serialize | XmlSerializer | MSDN: introducingxmlserialization | MSDN: overridingserializationofclasseswithxmlattributeoverridesclass | XmlAttributes | MSDN: controllingserializationbyxmlserializerwithattributes

System.Xml.Serialization.XmlAttributeOverrides Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Properties
Item Read-only

Overloaded:
Item[Type type] {get

Gets the object associated with the specified, base-class, type.
Item Read-only

Overloaded:
Item[Type type, string member] {get

Gets the object associated with the specified (base-class) type. The member parameter specifies the base-class member that is overridden.
Public Methods
Add Overloaded:
Add(Type type, XmlAttributes attributes)

Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden by the XmlAttributes object.
Add Overloaded:
Add(Type type, string member, XmlAttributes attributes)

Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden. The member parameter specifies the name of a member that will be overridden.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Serialization.XmlAttributeOverrides Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlAttributeOverrides();

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the object associated with the specified, base-class, type.
C# Syntax:
public XmlAttributes this[Type type] {get;}
Parameters:

type

The base-class class Type that is associated with the collection of attributes you want to retrieve.

Remarks
Use this overload to return an XmlAttributes object that contains attributes for an XmlRootAttribute or XmlTypeAttribute object.

If the XmlAttributes object contains objects that override an XmlArrayAttribute, XmlArrayItemAttribute, XmlElementAttribute, XmlEnumAttribute, or XmlAttributeAttribute, you must use the overload that specifies the overridden member as well as the type.

Example
The following example creates an XmlAttributeOverrides object, an XmlAttributes object, and an XmlRootAttribute object. The example assigns the XmlRootAttribute to the XmlAttributes.XmlRoot property of the XmlAttributes object, and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class to the XmlAttributeOverrides object. (In this example, the Type is Group.)
// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

public class Sample
{
public XmlSerializer CreateOverrider()
{
   // Create an XmlSerializer with overriding attributes.
   XmlAttributes attrs = new XmlAttributes();
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();
   
   XmlRootAttribute xRoot = new XmlRootAttribute();
   // Set a new Namespace and ElementName for the root element.
   xRoot.Namespace = "http://www.cpandl.com";
   xRoot.ElementName = "NewGroup";
   attrs.XmlRoot = xRoot;
   
   xOver.Add(typeof(Group), attrs);

   // Get the XmlAttributes object, based on the type.
   XmlAttributes tempAttrs;
   tempAttrs = xOver[typeof(Group)];

   // Print the Namespace and ElementName of the root.
   Console.WriteLine(tempAttrs.XmlRoot.Namespace);
   Console.WriteLine(tempAttrs.XmlRoot.ElementName);

   XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
   return xSer;
}
}

    
See also:
XmlAttributes | XmlSerializer

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the object associated with the specified (base-class) type. The member parameter specifies the base-class member that is overridden.
C# Syntax:
public XmlAttributes this[Type type, string member] {get;}
Parameters:

type

The base-class class Type that is associated with the collection of attributes you want.

member

The name of the overridden member that specifies the XmlAttributes to return.

Remarks
Use this overload to return an XmlAttributes object that contains objects that override an XmlArrayAttribute, XmlArrayItemAttribute, XmlAttributeAttribute, XmlElementAttribute, or XmlEnumAttribute. If the XmlAttributes object contains for an XmlRootAttribute or XmlTypeAttribute, you must use the overload that specifies only the overridden type.
Example
The following example creates an XmlAttributeOverrides object, an XmlAttributes, and an XmlAttributeAttribute object. The example assigns the XmlAttributeAttribute to the XmlAttributes.XmlAttribute property of the XmlAttributes object, and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class, and member name to the XmlAttributeOverrides object.
// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

public class Sample
{
public XmlSerializer CreateOverrider()
{
   // Create an XmlSerializer with overriding attributes.
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();

   /* Create an XmlAttributeAttribute object and set the 
   AttributeName property. */
   XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
   xAtt.AttributeName = "Code";

   /* Create a new XmlAttributes object and set the 
   XmlAttributeAttribute object to the XmlAttribute property. */
   XmlAttributes attrs = new XmlAttributes();
   attrs.XmlAttribute = xAtt;

   /* Add the XmlAttributes to the XmlAttributeOverrides object. The
   name of the overridden attribute must be specified. */
   xOver.Add(typeof(Group), "GroupCode", attrs);


   // Get the XmlAttributes object for the type and member.
   XmlAttributes tempAttrs;
   tempAttrs = xOver[typeof(Group), "GroupCode"];
   Console.WriteLine(tempAttrs.XmlAttribute.AttributeName);

   // Create the XmlSerializer instance and return it.
   XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
   return xSer;
}
}

    
See also:
XmlAttributes | XmlSerializer

Return to top


Overloaded Method: Add(
   Type type,
   XmlAttributes attributes
)
Summary
Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden by the XmlAttributes object.
C# Syntax:
public void Add(
   Type type,
   XmlAttributes attributes
);
Parameters:

type

The Type of the object that will be overridden.

attributes

An XmlAttributes object that represents the overriding attributes.

Remarks
The XmlAttributes object contains a union of attribute objects that cause the XmlSerializer to override its default serialization behavior for a set of objects. You choose the attribute objects to place in the XmlAttributes object, depending on the particular behaviors you want to override. For example, the XmlSerializer serializes a class member as an XML element by default. If you want the member to be serialized as an XmlAttribute instead, you would create an XmlAttributeAttribute, assign it to the XmlAttributes.XmlAttribute property of an XmlAttributes, and add the XmlAttributes object to the XmlAttributeOverrides object.

Use this overload to to override an XmlRootAttribute or XmlTypeAttribute.

Example
The following example serializes a class named Band which is derived from a class named Orchestra. The example creates an XmlRootAttribute object, and assigns it to the XmlAttributes.XmlRoot property of an XmlAttributes object. The example then calls the XmlAttributeOverrides.Add method--to add the XmlAttributes object to the XmlAttributeOverrides object.
using System;
using System.IO;
using System.Xml.Serialization;

/* This is the class that will be overridden. The XmlIncludeAttribute 
tells the XmlSerializer that the overriding type exists. */

[XmlInclude(typeof(Band))]
public class Orchestra
{
   public Instrument[] Instruments;
}   

// This is the overriding class.
public class Band:Orchestra
{
   public string BandName;
}

public class Instrument
{
   public string Name;
}

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


    public void SerializeObject(string filename)
    {
      /* Each object that is being overridden requires 
      an XmlAttributes object. */
      XmlAttributes attrs = new XmlAttributes();

      // An XmlRootAttribute allows overriding the Orchestra class.
      XmlRootAttribute xmlRoot = new XmlRootAttribute();

      // Set the object to the XmlAttribute.XmlRoot property.
      attrs.XmlRoot = xmlRoot;

      // Create an XmlAttributeOverrides object.
      XmlAttributeOverrides attrOverrides = 
      new XmlAttributeOverrides();

      // Add the XmlAttributes to the XmlAttributeOverrrides.
      attrOverrides.Add(typeof(Orchestra), attrs);

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

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create the object using the derived class.
      Band band = new Band();
      band.BandName = "NewBand";

      // Create an Instrument.
      Instrument i = new Instrument();
      i.Name = "Trumpet";
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;

      // Serialize the object.
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributes attrs = new XmlAttributes();
      XmlRootAttribute attr = new XmlRootAttribute();
      attrs.XmlRoot = attr;
      XmlAttributeOverrides attrOverrides = 
         new XmlAttributeOverrides();

      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      XmlSerializer s = 
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);

      // Deserialize the Band object.
      Band band = (Band) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      foreach(Instrument i in band.Instruments) 
      {
         Console.WriteLine(i.Name);
      }
   }
}


    
See also:
XmlAttributes | XmlSerializer

Return to top


Overloaded Method: Add(
   Type type,
   string member,
   XmlAttributes attributes
)
Summary
Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden. The member parameter specifies the name of a member that will be overridden.
C# Syntax:
public void Add(
   Type type,
   string member,
   XmlAttributes attributes
);
Parameters:

type

The Type of the object to override.

member

The name of the member to override.

attributes

An XmlAttributes object that represents the overriding attributes.

Remarks
The XmlAttributes object contains a union of attribute objects that cause the XmlSerializer to override its default serialization behavior for a set of objects. You choose the attribute objects to place in the XmlAttributes object, depending on the particular behaviors you want to override. For example, the XmlSerializer serializes a class member as an XML element by default. If you want the member to be serialized as an XmlAttribute instead, you would create an XmlAttributeAttribute, assign it to the XmlAttributes.XmlAttribute property of an XmlAttributes, and add the XmlAttributes object to the XmlAttributeOverrides object.

Use this method when attempting to override an XmlElementAttribute, XmlAttributeAttribute, XmlArrayAttribute, XmlArrayItemAttribute, or XmlIgnoreAttribute.

Example
The following example creates an XmlAttributeAttribute object, and assigns it to the XmlAttributes.XmlAttribute property of an XmlAttributes object. The example then adds the XmlAttributes object to an XmlAttributeOverrides object, before creating an XmlSerializer.
// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

public class Sample
{
public XmlSerializer CreateOverrider()
{
   // Create an XmlAttributeOverrides object. 
   XmlAttributeOverrides xOver = new XmlAttributeOverrides();

   /* Create an XmlAttributeAttribute to override the base class
   object's XmlAttributeAttribute object. Give the overriding object
   a new attribute name ("Code"). */
   XmlAttributeAttribute xAtt = new XmlAttributeAttribute();
   xAtt.AttributeName = "Code";

   /* Create an instance of the XmlAttributes class and set the 
   XmlAttribute property to the XmlAttributeAttribute object. */
   XmlAttributes attrs = new XmlAttributes();
   attrs.XmlAttribute = xAtt;

   /* Add the XmlAttributes object to the XmlAttributeOverrides
      and specify the type and member name to override. */
   xOver.Add(typeof(Group), "GroupCode", attrs);

   XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
   return xSer;
}
}

    
See also:
XmlAttributes | XmlSerializer

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlAttributeOverrides();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.