Export (0) Print
Expand All

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)

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue)] 
public class XmlIgnoreAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue) */ 
public class XmlIgnoreAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue) 
public class XmlIgnoreAttribute extends Attribute
Not applicable.

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 a null reference (Nothing in Visual Basic), 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.

NoteNote:

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.
   [XmlIgnore]
   public string Comment;

   // The XmlSerializer serializes this field.
   public string GroupName;
}
   

public class Group
{
    // The XmlSerializer ignores this field.
    /** @attribute XmlIgnore()
     */
    public String comment;
    // The XmlSerializer serializes this field.
    public String groupName;
} //Group

System.Object
   System.Attribute
    System.Xml.Serialization.XmlIgnoreAttribute

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft