XmlElementAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

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

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple := True)> _
Public Class XmlElementAttribute _
    Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple = true)]
public class XmlElementAttribute : Attribute

The XmlElementAttribute type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlElementAttribute() Initializes a new instance of the XmlElementAttribute class.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlElementAttribute(String) Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlElementAttribute(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 methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 XmlElementAttribute(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

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 DataType Gets or sets the XML Schema definition (XSD) data type of the XML element generated by the XmlSerializer.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ElementName Gets or sets the name of the generated XML element.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Form Gets or sets a value that indicates whether the element is qualified.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsNullable Gets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to nulla null reference (Nothing in Visual Basic) as an empty tag with the xsi:nil attribute set to true.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Namespace Gets or sets the namespace assigned to the XML element that results when the class is serialized.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Order Gets or sets the explicit order in which the elements are serialized or deserialized.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Type Gets or sets the object type used to represent the XML element.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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