XmlElementAttribute Class

July 28, 2014

Indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it.

System.Object
  System.Attribute
    System.Xml.Serialization.XmlElementAttribute

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

[AttributeUsageAttribute(AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.Property|AttributeTargets.ReturnValue, AllowMultiple = true)]
public class XmlElementAttribute : Attribute

The XmlElementAttribute type exposes the following members.

  NameDescription
Public methodXmlElementAttribute()Initializes a new instance of the XmlElementAttribute class.
Public methodXmlElementAttribute(String)Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.
Public methodXmlElementAttribute(Type)Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. This type is used by the XmlSerializer when serializing or deserializing object that contains it.
Public methodXmlElementAttribute(String, Type)Initializes a new instance of the XmlElementAttribute and specifies the name of the XML element and a derived type for the member to which the XmlElementAttribute is applied. This member type is used when the XmlSerializer serializes the object that contains it.
Top

  NameDescription
Public propertyDataTypeGets or sets the XML Schema definition (XSD) data type of the XML element generated by the XmlSerializer.
Public propertyElementNameGets or sets the name of the generated XML element.
Public propertyFormGets or sets a value that indicates whether the element is qualified.
Public propertyIsNullableGets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to null as an empty tag with the xsi:nil attribute set to true.
Public propertyNamespaceGets or sets the namespace assigned to the XML element that results when the class is serialized.
Public propertyOrderGets or sets the explicit order in which the elements are serialized or deserialized.
Public propertyTypeGets or sets the object type used to represent the XML element.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The XmlElementAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object.

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 XmlElementAttribute to public fields or public read/write properties to control characteristics of the XML elements, such as the element name and namespace.

The XmlElementAttribute can be applied multiple times to a field that returns an array of objects. The purpose of this is to specify (through the 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.

 public class Things{
    [XmlElement(DataType = typeof(string)),
    XmlElement(DataType = typeof(int))]
    public object[] StringsAndInts;
 }

This results in XML that might resemble the following.

 <Things>
    <string>Hello</string>
    <int>999</int>
    <string>World</string>
 </Things>

Note that when you apply the XmlElementAttribute multiple times without specifying an ElementName property value, the elements are named after the type of the acceptable objects.

If you apply the 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 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 XmlArrayAttribute and XmlArrayItemAttribute attributes to control how an array is serialized.)

You can set the 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 XmlElementAttribute.

If a field or property returns an ArrayList, you can apply multiple instances of the XmlElementAttribute to the member. For each instance, set the Type property to a type of object that can be inserted into the array.

NoteNote:

You can use the word XmlElement in your code instead of the longer XmlElementAttribute.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Show:
© 2014 Microsoft