Indicates that a public field or property represents an XML element when the System.Xml.Serialization.XmlSerializer serializes or deserializes the object that contains it.
See Also: XmlElementAttribute Members
The System.Xml.Serialization.XmlElementAttribute belongs to a family of attributes that controls how the System.Xml.Serialization.XmlSerializer serializes or deserializes an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.
An XML document usually contains XML elements, each of which consists of three parts: an opening tag with possible attributes, a closing tag, and the data between the tags. XML tags can be nested--that is, the data between tags can also be XML elements. This capacity of one element to enclose another allows the document to contain hierarchies of data. An XML element can also include attributes.
Apply the System.Xml.Serialization.XmlElementAttribute to public fields or public read/write properties to control characteristics of the XML elements such as the element name and namespace.
The System.Xml.Serialization.XmlElementAttribute can be applied multiple times to a field that returns an array of objects. The purpose of this is to specify (through the XmlElementAttribute.Type property) different types that can be inserted into the array. For example, the array in the following C# code accepts both strings and integers.
Example
public class Things{ [XmlElement(Type = typeof(string)), XmlElement(Type = typeof(int))] public object[] StringsAndInts; }
This results in XML that might resemble the following.
Example
<Things> <string>Hello</string> <int>999</int> <string>World</string> </Things>
Note that when you apply the System.Xml.Serialization.XmlElementAttribute multiple times without specifying an XmlElementAttribute.ElementName property value, the elements are named after the type of the acceptable objects.
If you apply the System.Xml.Serialization.XmlElementAttribute to a field or property that returns an array, the items in the array are encoded as a sequence of XML elements.
In contrast if an System.Xml.Serialization.XmlElementAttribute is not applied to such a field or property, the items in the array are encoded as a sequence of elements, nested under an element named after the field or property. (Use the System.Xml.Serialization.XmlArrayAttribute and System.Xml.Serialization.XmlArrayItemAttribute attributes to control how an array is serialized.)
You can set the XmlElementAttribute.Type property to specify a type that is derived from the type of the original field or property--that is, the field or property to which you have applied the System.Xml.Serialization.XmlElementAttribute.
If a field or property returns an ArrayList, you can apply multiple instances of the System.Xml.Serialization.XmlElementAttribute to the member. For each instance, set the XmlElementAttribute.Type property to a type of object that can be inserted into the array.
For more information about using attributes, see Extending Metadata Using Attributes.
You can use the word XmlElement in your code instead of the longer System.Xml.Serialization.XmlElementAttribute.