XmlAttributeAttribute Class

July 28, 2014

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

System.Object
  System.Attribute
    System.Xml.Serialization.XmlAttributeAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.Property Or AttributeTargets.ReturnValue)> _
Public Class XmlAttributeAttribute _
	Inherits Attribute

The XmlAttributeAttribute type exposes the following members.

  NameDescription
Public methodXmlAttributeAttributeInitializes a new instance of the XmlAttributeAttribute class.
Public methodXmlAttributeAttribute(String)Initializes a new instance of the XmlAttributeAttribute class and specifies the name of the generated XML attribute.
Public methodXmlAttributeAttribute(Type)Initializes a new instance of the XmlAttributeAttribute class.
Public methodXmlAttributeAttribute(String, Type)Initializes a new instance of the XmlAttributeAttribute class.
Top

  NameDescription
Public propertyAttributeNameGets or sets the name of the XML attribute.
Public propertyDataTypeGets or sets the XSD data type of the XML attribute generated by the XmlSerializer.
Public propertyFormGets or sets a value that indicates whether the XML attribute name generated by the XmlSerializer is qualified.
Public propertyNamespaceGets or sets the XML namespace of the XML attribute.
Public propertyTypeGets or sets the complex type of the XML attribute.
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 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.


[XmlAttribute("xml:lang")]
 public string Lang;
 // Set this to 'default' or 'preserve'.
 [XmlAttribute("space", 
 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

NoteNote:

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

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