Compartir a través de


Attributes That Control XML Serialization 

You can apply the attributes in the following table to classes and class members to control the way in which the XmlSerializer serializes or deserializes an instance of the class. To understand how these attributes control XML serialization, see Controlling XML Serialization Using Attributes.

These attributes can also be used to control the literal style SOAP messages generated by an XML Web service. For more information about applying these attributes to an XML Web services method, see XML Serialization with XML Web Services.

For more information about attributes, see Extending Metadata Using Attributes.

Attribute Applies to Specifies

XmlAnyAttributeAttribute

Public field, property, parameter, or return value that returns an array of XmlAttribute objects.

When deserializing, the array will be filled with XmlAttribute objects that represent all XML attributes unknown to the schema.

XmlAnyElementAttribute

Public field, property, parameter, or return value that returns an array of XmlElement objects.

When deserializing, the array is filled with XmlElement objects that represent all XML elements unknown to the schema.

XmlArrayAttribute

Public field, property, parameter, or return value that returns an array of complex objects.

The members of the array will be generated as members of an XML array.

XmlArrayItemAttribute

Public field, property, parameter, or return value that returns an array of complex objects.

The derived types that can be inserted into an array. Usually applied in conjunction with an XmlArrayAttribute.

XmlAttributeAttribute

Public field, property, parameter, or return value.

The member will be serialized as an XML attribute.

XmlChoiceIdentifierAttribute

Public field, property, parameter, or return value.

The member can be further disambiguated by using an enumeration.

XmlElementAttribute

Public field, property, parameter, or return value.

The field or property will be serialized as an XML element.

XmlEnumAttribute

Public field that is an enumeration identifier.

The element name of an enumeration member.

XmlIgnoreAttribute

Public properties and fields.

The property or field should be ignored when the containing class is serialized.

XmlIncludeAttribute

Public derived class declarations, and return values of public methods for Web Services Description Language (WSDL) documents.

The class should be included when generating schemas (to be recognized when serialized).

XmlRootAttribute

Public class declarations.

Controls XML serialization of the attribute target as an XML root element. Use the attribute to further specify the namespace and element name.

XmlTextAttribute

Public properties and fields.

The property or field should be serialized as XML text.

XmlTypeAttribute

Public class declarations.

The name and namespace of the XML type.

In addition to these attributes, which are all found in the System.Xml.Serialization namespace, you can also apply the System.ComponentModel.DefaultValueAttribute attribute to a field. The DefaultValueAttribute sets the value that will be automatically assigned to the member if no value is specified.

To control encoded SOAP XML serialization, see Attributes That Control Encoded SOAP Serialization.

See Also

Tasks

How to: Specify an Alternate Element Name for an XML Stream
How to: Serialize an Object
How to: Deserialize an Object

Reference

XmlSerializer

Concepts

Controlling XML Serialization Using Attributes

Other Resources

XML and SOAP Serialization