Export (0) Print
Expand All

XmlAttributes.XmlIgnore Property

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

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

public:
property bool XmlIgnore {
	bool get ();
	void set (bool value);
}
/** @property */
public boolean get_XmlIgnore ()

/** @property */
public void set_XmlIgnore (boolean value)

public function get XmlIgnore () : boolean

public function set XmlIgnore (value : boolean)

Property Value

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

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 and specify 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 is ignored. However you can override that behavior by creating an XmlAttributes object, setting its XmlIgnore property to false, 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.

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.

#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 ref 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 = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

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

   /* Use the XmlIgnore to instruct the XmlSerializer to ignore
      the GroupName instead. */
   attrs = gcnew XmlAttributes;
   attrs->XmlIgnore = true;
   xOver->Add( Group::typeid, "GroupName", attrs );
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,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 = gcnew Group;
   myGroup->GroupName = ".NET";
   myGroup->Comment = "My Comment...";

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

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

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

import System.*;
import System.IO.*;
import 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. */
    /** @attribute XmlIgnoreAttribute()
     */
    public String comment;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializeObject("IgnoreXml.xml");
    } //main

    // 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.set_XmlIgnore(false);
        xOver.Add(Group.class.ToType(), "Comment", attrs);

        /* Use the XmlIgnore to instruct the XmlSerializer to ignore
           the GroupName instead. */
        attrs = new XmlAttributes();
        attrs.set_XmlIgnore(true);
        xOver.Add(Group.class.ToType(), "GroupName", attrs);

        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider

    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();
    } //SerializeObject
} //Test

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft