Export (0) Print
Expand All

XmlAttributeOverrides.Add Method (Type, XmlAttributes)

Adds an XmlAttributes object to the collection of XmlAttributes objects. The type parameter specifies an object to be overridden by the XmlAttributes object.

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

public void Add (
	Type type,
	XmlAttributes attributes
)
public void Add (
	Type type, 
	XmlAttributes attributes
)
public function Add (
	type : Type, 
	attributes : XmlAttributes
)

Parameters

type

The Type of the object that is overridden.

attributes

An XmlAttributes object that represents the overriding attributes.

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 XM attribute instead, you would create an XmlAttributeAttribute, assign it to the XmlAttribute property of an XmlAttributes, and add the XmlAttributes object to the XmlAttributeOverrides object.

Use this overload to override an XmlRootAttribute or XmlTypeAttribute.

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 XmlRoot property of an XmlAttributes object. The example then calls the 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);
      }
   }
}


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

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

/** @attribute XmlInclude(Band.class)
 */
public class Orchestra
{
    public Instrument instruments[];
} //Orchestra

// This is the overriding class.
public class Band extends Orchestra
{
    public String bandName;
} //Band

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

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)
    {
        /* 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.set_XmlRoot(xmlRoot);

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

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

        // Create the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(), 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();
    } //SerializeObject

    public void DeserializeObject(String fileName)
    {
        XmlAttributes attrs = new XmlAttributes();
        XmlRootAttribute attr = new XmlRootAttribute();
        attrs.set_XmlRoot(attr);
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

        attrOverrides.Add(Orchestra.class.ToType(), "Instruments", attrs);
        XmlSerializer s =
            new XmlSerializer(Orchestra.class.ToType(), attrOverrides);
        FileStream fs = new FileStream(fileName, FileMode.Open);

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

        for (int iCtr = 0; iCtr < band.instruments.length; iCtr++) {
            Instrument i = band.instruments[iCtr];
            Console.WriteLine(i.name);
        }
    } //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

Community Additions

ADD
Show:
© 2014 Microsoft