XmlAttributes.XmlIgnore 속성

정의

XmlSerializer가 공용 필드 또는 읽기/쓰기 속성을 serialize하는지 여부를 지정하는 값을 가져오거나 설정합니다.

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가 필드 또는 속성을 serialize하지 않으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 라는 클래스를 직렬화 Group, 라는 멤버를 포함 하는 CommentXmlIgnoreAttribute 적용 됩니다. 만듭니다는 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

설명

기본적으로 모든 public 필드와 공용 읽기/쓰기 속성 하 여 serialize 되는 XmlSerializer합니다. 즉, 각 public 필드나 속성의 값은 XML 요소 또는 XML 문서 인스턴스에 XML 특성으로 유지 됩니다.

만들기 필드나 속성의 기본 serialization을 재정의 하는 XmlAttributes 개체를 설정 해당 XmlIgnore 속성을 true입니다. Add 개체는 XmlAttributeOverrides 개체 및 필드 또는 속성을 무시 하 고, 포함 된 개체의 형식 및 필드 또는 무시 하는 속성의 이름을 지정 합니다.

경우는 XmlIgnoreAttribute 적용 된 필드나 속성, 필드 또는 속성은 무시 됩니다. 만들어 동작을 재정의할 수는 있지만 XmlAttributes 개체, 설정 해당 XmlIgnore 속성을 false를 추가 하는 XmlAttributeOverrides 필드 또는 속성을 포함 하는 개체의 형식 및 이름을 지정 하는 개체는 필드 또는 속성입니다.

적용 대상

추가 정보