XmlAttributeAttribute Class

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

Specifies that the XmlSerializer must serialize the class member as an XML attribute.

Inheritance Hierarchy


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


<AttributeUsageAttribute(AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.Property Or AttributeTargets.ReturnValue)> _
Public Class XmlAttributeAttribute _
    Inherits Attribute
public class XmlAttributeAttribute : Attribute

The XmlAttributeAttribute type exposes the following members.


Name Description
XmlAttributeAttribute()()() Initializes a new instance of the XmlAttributeAttribute class.
XmlAttributeAttribute(String) Initializes a new instance of the XmlAttributeAttribute class and specifies the name of the generated XML attribute.
XmlAttributeAttribute(Type) Initializes a new instance of the XmlAttributeAttribute class.
XmlAttributeAttribute(String, Type) Initializes a new instance of the XmlAttributeAttribute class.



Name Description
AttributeName Gets or sets the name of the XML attribute.
DataType Gets or sets the XSD data type of the XML attribute generated by the XmlSerializer.
Form Gets or sets a value that indicates whether the XML attribute name generated by the XmlSerializer is qualified.
Namespace Gets or sets the XML namespace of the XML attribute.
Type Gets or sets the complex type of the XML attribute.



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.)



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

When applied to a public field or property, the XmlAttributeAttribute informs the XmlSerializer to serialize the member as an XML attribute. By default, the XmlSerializer serializes public fields and properties as XML elements.

You can assign the XmlAttributeAttribute only to public fields or public properties that return a value (or array of values) that can be mapped to one of the XML Schema definition (XSD) language simple types (including all built-in data types derived from the XSD anySimpleType type). The possible types include any that can be mapped to the XSD simple types, including Guid, Char, and enumerations. See the DataType property for a list of XSD types and how they are mapped to .NET data types.

There are two special attributes that can be set with the XmlAttributeAttribute: the xml:lang (specifies language) and xml:space (specifies how to handle white space) attributes. These attributes are intended to convey information that is relevant only to an application that is processing the XML. Examples of setting these are shown in the following code.

 public string Lang;
 // Set this to 'default' or 'preserve'.
 Namespace = "http://www.w3.org/XML/1998/namespace")]
 public string Space 

 [Visual Basic]
 <XmlAttribute("xml:lang")> _
 Public Lang As String 
 ' Set this to 'default' or 'preserve'.
 <XmlAttribute("space", _
 Namespace:= "http://www.w3.org/XML/1998/namespace")> _
 Public Space As String


You can use the word XmlAttribute in your code instead of the longer XmlAttributeAttribute.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0


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


System.Xml.Serialization Namespace