This documentation is archived and is not being maintained.

XmlIgnoreAttribute Class

Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value.

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

public class XmlIgnoreAttribute : Attribute

The XmlIgnoreAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. If you apply the XmlIgnoreAttribute to any member of a class, the 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 XmlIgnoreAttribute by creating an XmlAttributes object, and setting its XmlIgnore property to false. You must Add the XmlAttributes object to an instance of the XmlAttributeOverrides class. Lastly, you must use the XmlAttributeOverrides object to construct an instance of the XmlSerializer class before you call the Serialize or Deserialize methods.

The [XML Schema Definition Tool (Xsd.exe) occasionally generates the 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 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 XmlIgnoreAttribute to it.

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


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

The following example shows a class named Group, which contains a field named Comment. The example assigns the XmlIgnoreAttribute to the field, thereby instructing the XmlSerializer to ignore the field when serializing or deserializing an instance of the class.

public class Group
   // The XmlSerializer ignores this field.
   public string Comment;

   // The XmlSerializer serializes this field. 
   public string GroupName;
public __gc class Group
   // The XmlSerializer ignores this field.
   String* Comment;

   // The XmlSerializer serializes this field.
   String* GroupName;


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0