XmlElementAttributes Class
Represents a collection of XmlElementAttribute objects used by the XmlSerializer to override the default way it serializes a class.
Assembly: System.Xml (in System.Xml.dll)
The XmlElementAttributes type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | XmlElementAttributes | Initializes a new instance of the XmlElementAttributes class. |
| Name | Description | |
|---|---|---|
![]() ![]() | Capacity | Gets or sets the number of elements that the CollectionBase can contain. (Inherited from CollectionBase.) |
![]() ![]() | Count | Gets the number of elements contained in the CollectionBase instance. This property cannot be overridden. (Inherited from CollectionBase.) |
![]() ![]() | InnerList | Gets an ArrayList containing the list of elements in the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() ![]() | Item | Gets or sets an XmlElementAttribute from the collection. |
![]() ![]() | List | Gets an IList containing the list of elements in the CollectionBase instance. (Inherited from CollectionBase.) |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | Add | Adds an XmlElementAttribute to the collection. |
![]() ![]() | Clear | Removes all objects from the CollectionBase instance. This method cannot be overridden. (Inherited from CollectionBase.) |
![]() ![]() ![]() | Contains | Gets a value that specifies whether the collection contains the specified object. |
![]() ![]() ![]() | CopyTo | Copies the XmlElementAttributes, or a portion of it to a one-dimensional array. |
![]() ![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | GetEnumerator | Returns an enumerator that iterates through the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() ![]() | IndexOf | Gets the index of the specified XmlElementAttribute. |
![]() ![]() ![]() | Insert | Inserts an XmlElementAttribute into the collection. |
![]() ![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() | OnClear | Performs additional custom processes when clearing the contents of the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnClearComplete | Performs additional custom processes after clearing the contents of the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnInsert | Performs additional custom processes before inserting a new element into the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnInsertComplete | Performs additional custom processes after inserting a new element into the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnRemove | Performs additional custom processes when removing an element from the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnRemoveComplete | Performs additional custom processes after removing an element from the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnSet | Performs additional custom processes before setting a value in the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnSetComplete | Performs additional custom processes after setting a value in the CollectionBase instance. (Inherited from CollectionBase.) |
![]() ![]() | OnValidate | Performs additional custom processes when validating a value. (Inherited from CollectionBase.) |
![]() ![]() ![]() | Remove | Removes the specified object from the collection. |
![]() ![]() | RemoveAt | Removes the element at the specified index of the CollectionBase instance. This method is not overridable. (Inherited from CollectionBase.) |
![]() ![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | ICollection.CopyTo | Copies the entire CollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from CollectionBase.) |
![]() ![]() ![]() | ICollection.IsSynchronized | Gets a value indicating whether access to the CollectionBase is synchronized (thread safe). (Inherited from CollectionBase.) |
![]() ![]() ![]() | ICollection.SyncRoot | Gets an object that can be used to synchronize access to the CollectionBase. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.Add | Adds an object to the end of the CollectionBase. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.Contains | Determines whether the CollectionBase contains a specific element. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.IndexOf | Searches for the specified Object and returns the zero-based index of the first occurrence within the entire CollectionBase. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.Insert | Inserts an element into the CollectionBase at the specified index. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.IsFixedSize | Gets a value indicating whether the CollectionBase has a fixed size. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.IsReadOnly | Gets a value indicating whether the CollectionBase is read-only. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.Item | Gets or sets the element at the specified index. (Inherited from CollectionBase.) |
![]() ![]() ![]() | IList.Remove | Removes the first occurrence of a specific object from the CollectionBase. (Inherited from CollectionBase.) |
The XmlElementAttributes is returned by the XmlElements property of the XmlAttributes class. By using the XmlAttributeOverrides class and the XmlAttributes class, you can override the default way that the XmlSerializer serializes a class.
The following example serializes the Transportation class, which contains a single field named Vehicles that returns an ArrayList. The example first applies two instances of the XmlElementAttribute class to the Vehicles field that specifies the types of objects the XmlSerializer inserts into the array. The example then creates two XmlElementAttribute objects to override the behavior of the attributes applied to the Vehicles property. The two overriding objects are added to the XmlElementAttributes collection of an XmlAttributes. Lastly, the example adds the XmlAttributes to an XmlAttributeOverrides, allowing the XmlSerializer to insert the new object types into the ArrayList returned by the Vehicles field.
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; public class Transportation { // Override these two XmlElementAttributes. [XmlElement(typeof(Car)), XmlElement(typeof(Plane))] public ArrayList Vehicles; } public class Car { public string Name; } public class Plane { public string Name; } public class Truck { public string Name; } public class Train { public string Name; } public class Test { public static void Main() { Test t = new Test(); t.SerializeObject("OverrideElement.xml"); } public XmlSerializer CreateOverrider() { // Create XmlAtrributes and XmlAttributeOverrides instances. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlElementAttributes object to override one of the attributes applied to the Vehicles property. */ XmlElementAttribute xElement1 = new XmlElementAttribute(typeof(Truck)); // Add the XmlElementAttribute to the collection. attrs.XmlElements.Add(xElement1); /* Create a second XmlElementAttribute and add it to the collection. */ XmlElementAttribute xElement2 = new XmlElementAttribute(typeof(Train)); attrs.XmlElements.Add(xElement2); /* Add the XmlAttributes to the XmlAttributeOverrides, specifying the member to override. */ xOver.Add(typeof(Transportation), "Vehicles", attrs); // Create the XmlSerializer, and return it. XmlSerializer xSer = new XmlSerializer (typeof(Transportation), xOver); return xSer; } public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = CreateOverrider(); // Create the object. Transportation myTransportation = new Transportation(); /* Create two new, overriding objects that can be inserted into the Vehicles array. */ myTransportation.Vehicles = new ArrayList(); Truck myTruck = new Truck(); myTruck.Name = "MyTruck"; Train myTrain = new Train(); myTrain.Name = "MyTrain"; myTransportation.Vehicles.Add(myTruck); myTransportation.Vehicles.Add(myTrain); TextWriter writer = new StreamWriter(filename); xSer.Serialize(writer, myTransportation); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.








