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.

For a list of all members of this type, see XmlIgnoreAttribute Members.

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

[Visual Basic]
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlIgnoreAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlIgnoreAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
   AttributeTargets::ReturnValue)]
public __gc class XmlIgnoreAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlIgnoreAttribute extends Attribute

Thread Safety

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

Remarks

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 need not occur (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.

Note   In your code, you can use the word XmlIgnore instead of the longer XmlIgnoreAttribute.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Class Group
    ' the XmlSerializer ignores this field.
    <XmlIgnore()> Public Comment As String
    
    ' The XmlSerializer serializes this field.
    Public GroupName As String
End Class


[C#] 
public class Group
{
   // The XmlSerializer ignores this field.
   [XmlIgnore]
   public string Comment;

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

[C++] 
public __gc class Group
{
public:
   // The XmlSerializer ignores this field.
   [XmlIgnore]
   String* Comment;

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

XmlIgnoreAttribute Members | System.Xml.Serialization Namespace | XmlAttributeOverrides | XmlAttributes | XmlIgnore | XmlSerializer | Introducing XML Serialization | Overriding XML Serialization | XmlAttributes | Controlling XML Serialization Using Attributes | Examples of XML Serialization

Show:
© 2014 Microsoft