public class XmlAttributeOverrides
|
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:
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); } } }
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
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. |
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. |
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:
public XmlAttributeOverrides(); |
public XmlAttributes this[Type type] {get;}
|
type
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.
// 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; } }
public XmlAttributes this[Type type, string member] {get;}
|
type
member
// 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; } }
public void Add( |
type
attributes
Use this overload to to override an XmlRootAttribute or XmlTypeAttribute.
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); } } }
public void Add( |
type
member
attributes
Use this method when attempting to override an XmlElementAttribute, XmlAttributeAttribute, XmlArrayAttribute, XmlArrayItemAttribute, or XmlIgnoreAttribute.
// 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; } }
~XmlAttributeOverrides(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |