XmlAttributes.XmlIgnore 属性

定义

获取或设置一个值,该值指定 XmlSerializer 是否串行化公共字段或公共读/写属性。

public:
 property bool XmlIgnore { bool get(); void set(bool value); };
public bool XmlIgnore { get; set; }
member this.XmlIgnore : bool with get, set
Public Property XmlIgnore As Boolean

属性值

Boolean

如果 XmlSerializer 不得序列化字段或属性,则为 true;否则为 false

示例

The following example serializes a class named Group, which contains a member named Comment to which the XmlIgnoreAttribute is applied. 该示例创建一个 XmlAttributes 对象,并将属性设置为 XmlIgnore false,从而重写该 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" );
}
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();
   }
}
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

注解

默认情况下,所有公共字段和公共读/写属性都由该 XmlSerializer属性序列化。 也就是说,每个公共字段或属性的值将作为 XML 元素或 XML 属性保存在 XML 文档实例中。

若要重写字段或属性的默认序列化,请创建一个 XmlAttributes 对象,并将其属性设置为 XmlIgnore trueAdd 对象到对象 XmlAttributeOverrides ,并指定要忽略的字段或属性的对象的类型,以及要忽略的字段或属性的名称。

XmlIgnoreAttribute如果应用于字段或属性,则忽略该字段或属性。 但是,可以通过创建对象XmlAttributes、将其属性设置为XmlIgnorefalse该对象、将其添加到XmlAttributeOverrides指定包含字段或属性的对象的类型以及字段或属性的名称来替代该行为。

适用于

另请参阅