다음을 통해 공유


XmlAttributes.XmlArrayItems 속성

정의

XmlSerializer가 공용 필드 또는 읽기/쓰기 속성에 의해 반환된 배열 내에 삽입된 항목을 serialize하는 방식을 지정하는 개체 컬렉션을 가져오거나 설정합니다.

public:
 property System::Xml::Serialization::XmlArrayItemAttributes ^ XmlArrayItems { System::Xml::Serialization::XmlArrayItemAttributes ^ get(); };
public System.Xml.Serialization.XmlArrayItemAttributes XmlArrayItems { get; }
member this.XmlArrayItems : System.Xml.Serialization.XmlArrayItemAttributes
Public ReadOnly Property XmlArrayItems As XmlArrayItemAttributes

속성 값

XmlArrayItemAttributes

XmlArrayItemAttributes 개체의 컬렉션을 포함하는 XmlArrayItemAttribute 개체입니다.

예제

다음 예제에서는 명명 된 필드를 포함 하는 클래스를 직렬화 Members 개체의 배열을 반환 합니다. 두 개의 XmlArrayItemAttribute 개체를 필드에 명명 된 기본 클래스에서 파생 되는 개체를 적용할 수 있도록 만들어진 Member합니다. 각 개체에 추가 합니다 XmlAttributes 를 통해를 XmlArrayItems 속성.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Member
{
public:
   String^ MemberName;
};

// This is the class that will be serialized.
public ref class Group
{
public:
   array<Member^>^Members;
};

public ref class NewMember: public Member
{
public:
   int MemberID;
};

public ref class RetiredMember: public NewMember
{
public:
   DateTime RetireDate;
};

// Return an XmlSerializer used for overriding. 
XmlSerializer^ CreateOverrider()
{
   // Create XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAttrs = gcnew XmlAttributes;

   // Add an override for the XmlArrayItem.    
   XmlArrayItemAttribute^ xArrayItem = gcnew XmlArrayItemAttribute( NewMember::typeid );
   xArrayItem->Namespace = "http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add( xArrayItem );

   // Add a second override.
   XmlArrayItemAttribute^ xArrayItem2 = gcnew XmlArrayItemAttribute( RetiredMember::typeid );
   xArrayItem2->Namespace = "http://www.cpandl.com";
   xAttrs->XmlArrayItems->Add( xArrayItem2 );

   // Add all overrides to XmlAttribueOverrides object.
   xOver->Add( Group::typeid, "Members", xAttrs );

   // Create the XmlSerializer and return it.
   return gcnew XmlSerializer( Group::typeid,xOver );
}

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

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

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   NewMember^ m = gcnew NewMember;
   m->MemberName = "Paul";
   m->MemberID = 2;

   // Create a second member.
   RetiredMember^ m2 = gcnew RetiredMember;
   m2->MemberName = "Renaldo";
   m2->MemberID = 23;
   m2->RetireDate = DateTime(2000,10,10);
   array<Member^>^temp = {m,m2};
   myGroup->Members = temp;

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

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   System::Collections::IEnumerator^ myEnum = myGroup->Members->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Member^ m = safe_cast<Member^>(myEnum->Current);
      Console::WriteLine( m->MemberName );
   }
}

int main()
{
   SerializeObject( "OverrideArrayItem.xml" );
   DeserializeObject( "OverrideArrayItem.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
    public Member [] Members;
}

public class Member
{
    public string MemberName;
}

public class NewMember:Member
{
    public int MemberID;
}

public class RetiredMember:NewMember
{
    public DateTime RetireDate;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideArrayItem.xml");
      test.DeserializeObject("OverrideArrayItem.xml");
   }

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

      // Add an override for the XmlArrayItem.
      XmlArrayItemAttribute xArrayItem =
      new XmlArrayItemAttribute(typeof(NewMember));
      xArrayItem.Namespace = "http://www.cpandl.com";
      xAttrs.XmlArrayItems.Add(xArrayItem);

      // Add a second override.
      XmlArrayItemAttribute xArrayItem2 =
      new XmlArrayItemAttribute(typeof(RetiredMember));
      xArrayItem2.Namespace = "http://www.cpandl.com";
      xAttrs.XmlArrayItems.Add(xArrayItem2);

      // Add all overrides to XmlAttribueOverrides object.
      xOver.Add(typeof(Group), "Members", xAttrs);

      // Create the XmlSerializer and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      NewMember m = new NewMember();
      m.MemberName = "Paul";
      m.MemberID = 2;

      // Create a second member.
      RetiredMember m2 = new RetiredMember();
      m2.MemberName = "Renaldo";
      m2.MemberID = 23;
      m2.RetireDate = new DateTime(2000, 10,10);

      myGroup.Members = new Member[2] {m, m2};

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group)
      mySerializer.Deserialize(fs);
      foreach(Member m in myGroup.Members)
      {
          Console.WriteLine(m.MemberName);
      }
   }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class Group
    Public Members() As Member
End Class

Public Class Member
    Public MemberName As String
End Class

Public Class NewMember
    Inherits Member
    Public MemberID As Integer
End Class

Public Class RetiredMember
    Inherits NewMember
    Public RetireDate As DateTime
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideArrayItem.xml")
        test.DeserializeObject("OverrideArrayItem.xml")
    End Sub    
    
    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Add an override for the XmlArrayItem.    
        Dim xArrayItem As New XmlArrayItemAttribute(GetType(NewMember))
        xArrayItem.Namespace = "http://www.cpandl.com"
        xAttrs.XmlArrayItems.Add(xArrayItem)
        
        ' Add a second override.
        Dim xArrayItem2 As New XmlArrayItemAttribute(GetType(RetiredMember))
        xArrayItem2.Namespace = "http://www.cpandl.com"
        xAttrs.XmlArrayItems.Add(xArrayItem2)
        
        ' Add all overrides to XmlAttribueOverrides object.
        xOver.Add(GetType(Group), "Members", xAttrs)
        
        ' Create the XmlSerializer and return it.
        Return New XmlSerializer(GetType(Group), xOver)
    End Function 
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        Dim m As New NewMember()
        m.MemberName = "Paul"
        m.MemberID = 2
        
        ' Create a second member.
        Dim m2 As New RetiredMember()
        m2.MemberName = "Renaldo"
        m2.MemberID = 23
        m2.RetireDate = New DateTime(2000, 10, 10)
        
        myGroup.Members = New Member(1) {m, m2}
        
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        Dim m As Member
        For Each m In  myGroup.Members
            Console.WriteLine(m.MemberName)
        Next m
    End Sub
End Class

설명

XmlArrayItems 속성을 사용 하면 공용 필드 또는 공용 읽기/쓰기 속성에 의해 반환 된 배열에 삽입할 수 있는 파생된 된 형식을 지정할 수 있습니다. 만들기 필드나 속성을 허용 하려는 각 새 형식에 대해를 XmlArrayItemAttribute 개체 및 Add 되도록를 XmlArrayItemAttributes)에서 반환 되는 XmlArrayItems 속성. (새 형식 필드 또는 속성에 이미 적용 된 형식에서 파생 되어야 합니다.) AddXmlAttributes 개체는 XmlAttributeOverrides 개체 및 필드 또는 속성을 포함 하는 개체의 형식 및 필드 또는 속성의 이름을 지정 합니다. 생성을 XmlSerializer 사용 하 여 합니다 XmlAttributeOverrides 호출 하기 전에 개체 Serialize 또는 Deserialize 메서드.

적용 대상