This documentation is archived and is not being maintained.

XmlAttributes.XmlIgnore Property

Gets or sets a value that specifies whether or not the XmlSerializer serializes a public field or public read/write property.

[Visual Basic]
Public Property XmlIgnore As Boolean
[C#]
public bool XmlIgnore {get; set;}
[C++]
public: __property bool get_XmlIgnore();
public: __property void set_XmlIgnore(bool);
[JScript]
public function get XmlIgnore() : Boolean;
public function set XmlIgnore(Boolean);

Property Value

true if the XmlSerializer should not serialize the field or property; otherwise, false.

Remarks

By default, all public fields and public read/write properties are serialized by the XmlSerializer. That is, the value of each public field or property is persisted as an XML element or XML attribute in an XML-document instance.

To override the default serialization of a field or property, create an XmlAttributes object, and set its XmlIgnore property to true. Add the object to an XmlAttributeOverrides object specifying the type of the object that contains the field or property to ignore, and the name of the field or property to ignore.

If an XmlIgnoreAttribute is applied to a field or property, the field or property will be ignored. However you can override that behavior by creating an XmlAttributes object, setting its XmlIgnore property to false, and adding it to an XmlAttributeOverrides object specifying the type of the object that contains the field or property, and the name of the field or property.

Example

[Visual Basic, C#, C++] The following example serializes a class named Group,which contains a member named Comment to which the XmlIgnoreAttribute is applied. The example creates an XmlAttributes object, and sets the XmlIgnore property to false, thereby overriding the XmlIgnoreAttribute.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml.Serialization


' This is the class that will be serialized. 
Public Class Group
    ' The GroupName value will be serialized--unless it's overridden.
    Public GroupName As String
    
    ' This field will be ignored when serialized--
    '  unless it's overridden.
    <XmlIgnoreAttribute()> Public Comment As String
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("IgnoreXml.xml")
    End Sub
    
    
    ' Return an XmlSerializer used for overriding.
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Setting XmlIgnore to false overrides the XmlIgnoreAttribute
        ' applied to the Comment field. Thus it will be serialized.
        attrs.XmlIgnore = False
        xOver.Add(GetType(Group), "Comment", attrs)
        
        ' Use the XmlIgnore to instruct the XmlSerializer to ignore
        ' the GroupName instead. 
        attrs = New XmlAttributes()
        attrs.XmlIgnore = True
        xOver.Add(GetType(Group), "GroupName", attrs)
        
        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object to serialize and set its properties.
        Dim myGroup As New Group()
        myGroup.GroupName = ".NET"
        myGroup.Comment = "My Comment..."
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class


[C#] 
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be serialized. 
public class Group
{
   // The GroupName value will be serialized--unless it's overridden.
   public string GroupName;

   /* This field will be ignored when serialized--
      unless it's overridden. */
   [XmlIgnoreAttribute]
   public string Comment;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("IgnoreXml.xml");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      /* Setting XmlIgnore to false overrides the XmlIgnoreAttribute
         applied to the Comment field. Thus it will be serialized.*/
      attrs.XmlIgnore = false;
      xOver.Add(typeof(Group), "Comment", attrs);

      /* Use the XmlIgnore to instruct the XmlSerializer to ignore
         the GroupName instead. */
      attrs = new XmlAttributes();
      attrs.XmlIgnore = true;
      xOver.Add(typeof(Group), "GroupName", attrs);
      
      XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
      return xSer;
   }

   public void SerializeObject(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create the object to serialize and set its properties.
      Group myGroup = new Group();
      myGroup.GroupName = ".NET";
      myGroup.Comment = "My Comment...";
   
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Serialize the object and close the TextWriter.
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

// This is the class that will be serialized. 
public __gc class Group
{
public:
   // The GroupName value will be serialized--unless it's overridden.
   String* GroupName;

   /* This field will be ignored when serialized--
   unless it's overridden. */
   [XmlIgnoreAttribute]
   String* Comment;
};

// Return an XmlSerializer used for overriding.
XmlSerializer* CreateOverrider()
{
   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides* xOver = new XmlAttributeOverrides();
   XmlAttributes* attrs = new XmlAttributes();

   /* Setting XmlIgnore to false overrides the XmlIgnoreAttribute
   applied to the Comment field. Thus it will be serialized.*/
   attrs->XmlIgnore = false;
   xOver->Add(__typeof(Group), S"Comment", attrs);

   /* Use the XmlIgnore to instruct the XmlSerializer to ignore
   the GroupName instead. */
   attrs = new XmlAttributes();
   attrs->XmlIgnore = true;
   xOver->Add(__typeof(Group), S"GroupName", attrs);

   XmlSerializer* xSer = new XmlSerializer(__typeof(Group), xOver);
   return xSer;
}

void SerializeObject(String* filename)
{
   // Create an XmlSerializer instance.
   XmlSerializer* xSer = CreateOverrider();

   // Create the object to serialize and set its properties.
   Group* myGroup = new Group();
   myGroup->GroupName = S".NET";
   myGroup->Comment = S"My Comment...";

   // Writing the file requires a TextWriter.
   TextWriter* writer = new StreamWriter(filename);

   // Serialize the object and close the TextWriter.
   xSer->Serialize(writer, myGroup);
   writer->Close();
}

int main()
{
   SerializeObject(S"IgnoreXml.xml");
}

[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

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

See Also

XmlAttributes Class | XmlAttributes Members | System.Xml.Serialization Namespace | XmlIgnoreAttribute

Show: