System.Xml.Serialization.XmlIgnoreAttribute Class

Instructs the XmlSerializer.Serialize(System.IO.TextWriter, object) method of the System.Xml.Serialization.XmlSerializer not to serialize the public field or public read/write property value.

See Also: XmlIgnoreAttribute Members

Syntax

[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.ReturnValue | System.AttributeTargets.All)]
public class XmlIgnoreAttribute : Attribute

Remarks

The System.Xml.Serialization.XmlIgnoreAttribute belongs to a family of attributes that controls how the System.Xml.Serialization.XmlSerializer serializes or deserializes an object. If you apply the System.Xml.Serialization.XmlIgnoreAttribute to any member of a class, the System.Xml.Serialization.XmlSerializer ignores the member when serializing or deserializing an instance of the class. For a complete list of similar attributes, see Attributes That Control XML Serialization.

You can override the behavior caused by the System.Xml.Serialization.XmlIgnoreAttribute by creating an System.Xml.Serialization.XmlAttributes object, and setting its XmlAttributes.XmlIgnore property to false. You must XmlAttributeOverrides.Add(Type, XmlAttributes) the System.Xml.Serialization.XmlAttributes object to an instance of the System.Xml.Serialization.XmlAttributeOverrides class. Lastly, you must use the System.Xml.Serialization.XmlAttributeOverrides object to construct an instance of the System.Xml.Serialization.XmlSerializer class before you call the XmlSerializer.Serialize(System.IO.TextWriter, object) or XmlSerializer.Deserialize(System.IO.Stream) methods.

The [XML Schema Definition Tool (Xsd.exe) occasionally generates the System.Xml.Serialization.XmlIgnoreAttribute when creating classes from a schema file (.xsd). This behavior occurs because value types cannot be set to null, but all XML data types can be. Therefore, the tool creates two fields when it encounters an XML type that maps to a value type: one to hold the value and another special field that takes the form of fieldnameSpecified, where the fieldname is replaced by the name of the field or property. Notice, however, that this special field is generated only when the schema specifies that the element has no minimum occurrence (minOccurs = "0") and that the element has no default value. The System.Xml.Serialization.XmlSerializer sets and checks this special field to determine whether a value has been set for the field or property. Because the special field must not be serialized, the tool applies the System.Xml.Serialization.XmlIgnoreAttribute to it.

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

Note:

You can use the word XmlIgnore in your code instead of the longer System.Xml.Serialization.XmlIgnoreAttribute.

Requirements

Namespace: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0