Export (0) Print
Expand All

XmlElementAttribute Class

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)

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

The XmlElementAttribute type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360XmlElementAttributeInitializes a new instance of the XmlElementAttribute class.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360XmlElementAttribute(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 360XmlElementAttribute(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 360XmlElementAttribute(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 propertySupported by Silverlight for Windows PhoneSupported by Xbox 360DataTypeGets 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 360ElementNameGets or sets the name of the generated XML element.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360FormGets or sets a value that indicates whether the element is qualified.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsNullableGets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to Nothing as an empty tag with the xsi:nil attribute set to true.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360NamespaceGets 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 360OrderGets or sets the explicit order in which the elements are serialized or deserialized.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TypeGets or sets the object type used to represent the XML element.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualsInfrastructure. 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 360FinalizeAllows 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 360GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MatchWhen 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 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns 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.

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

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

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

Community Additions

ADD
Show:
© 2014 Microsoft