XmlElementAttribute Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

The XmlElementAttribute type exposes the following members.

Constructors

Name Description
XmlElementAttribute()()() Initializes a new instance of the XmlElementAttribute class.
XmlElementAttribute(String) Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.
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.
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
DataType Gets or sets the XML Schema definition (XSD) data type of the XML element generated by the XmlSerializer.
ElementName Gets or sets the name of the generated XML element.
Form Gets or sets a value that indicates whether the element is qualified.
IsNullable Gets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to nullNothingnullptra null reference (Nothing in Visual Basic) as an empty tag with the xsi:nil attribute set to true.
Namespace Gets or sets the namespace assigned to the XML element that results when the class is serialized.
Order Gets or sets the explicit order in which the elements are serialized or deserialized.
Type Gets or sets the object type used to represent the XML element.

Top

Methods

Name Description
Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
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.)
GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
GetType Gets the Type of the current instance. (Inherited from Object.)
Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
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.

Note

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

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

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.

See Also

Reference

System.Xml.Serialization Namespace

XmlArrayAttribute

XmlAttributeOverrides

XmlAttributes

XmlElementAttributes

XmlElements

XmlRootAttribute

XmlSerializer