The XmlAttributeAttribute belongs to a family of attributes that controls how the XmlSerializer serializes, or deserializes, an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.
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 language (XSD) simple types (including all built-in datatypes 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 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
For more information about using attributes, see Extending Metadata Using Attributes.
Note: |
|---|
You can use the word
XmlAttribute in your code instead of the longer XmlAttributeAttribute.
|