XmlArrayItemAttribute Sınıf

Tanım

' nin serileştirilmiş bir diziye yerleştirebileceği türetilmiş türleri XmlSerializer belirten bir özniteliği temsil eder.

public ref class XmlArrayItemAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlArrayItemAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlArrayItemAttribute = class
    inherit Attribute
Public Class XmlArrayItemAttribute
Inherits Attribute
Devralma
XmlArrayItemAttribute
Öznitelikler

Örnekler

Aşağıdaki örnek, bir nesne dizisi Employee döndüren adlı Group bir alan içeren adlı Employees sınıfı serileştirir. Örnek, alanını uygular XmlArrayItemAttribute ve böylece hem temel sınıf () türünde hem de türetilmiş sınıf türündeManager (Employee) nesneleri serileştirilmiş diziye ekleyebileceğini bildirirXmlSerializer.

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Employee
{
public:
   String^ Name;
};

public ref class Manager: public Employee
{
public:
   int Level;
};

public ref class Group
{
public:

   /* The XmlArrayItemAttribute allows the XmlSerializer to insert
      both the base type (Employee) and derived type (Manager) 
      into serialized arrays. */

   [XmlArrayItem(Manager::typeid),
   XmlArrayItem(Employee::typeid)]
   array<Employee^>^Employees;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */

   [XmlArray]
   [XmlArrayItem(Int32::typeid,
   ElementName="MyNumber"),
   XmlArrayItem(String::typeid,
   ElementName="MyString"),
   XmlArrayItem(Manager::typeid)]
   array<Object^>^ExtraInfo;
};

void SerializeObject( String^ filename )
{
   // Creates a new XmlSerializer.
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   Group^ group = gcnew Group;
   Manager^ manager = gcnew Manager;
   Employee^ emp1 = gcnew Employee;
   Employee^ emp2 = gcnew Employee;
   manager->Name = "Consuela";
   manager->Level = 3;
   emp1->Name = "Seiko";
   emp2->Name = "Martina";
   array<Employee^>^emps = {manager,emp1,emp2};
   group->Employees = emps;

   // Creates an int and a string and assigns to ExtraInfo.
   array<Object^>^temp = {43,"Extra",manager};
   group->ExtraInfo = temp;

   // Serializes the object, and closes the StreamWriter.
   s->Serialize( writer, group );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   XmlSerializer^ x = gcnew XmlSerializer( Group::typeid );
   Group^ g = dynamic_cast<Group^>(x->Deserialize( fs ));
   Console::WriteLine( "Members:" );
   System::Collections::IEnumerator^ myEnum = g->Employees->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Employee^ e = safe_cast<Employee^>(myEnum->Current);
      Console::WriteLine( "\t{0}", e->Name );
   }
}

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

public class Group
{
   /* The XmlArrayItemAttribute allows the XmlSerializer to insert
      both the base type (Employee) and derived type (Manager)
      into serialized arrays. */

   [XmlArrayItem(typeof(Manager)),
   XmlArrayItem(typeof(Employee))]
   public Employee[] Employees;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (typeof(int),
   ElementName = "MyNumber"),
   XmlArrayItem (typeof(string),
   ElementName = "MyString"),
   XmlArrayItem(typeof(Manager))]
   public object [] ExtraInfo;
}

public class Employee
{
   public string Name;
}

public class Manager:Employee{
   public int Level;
}

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

   public void SerializeObject(string filename)
   {
      // Creates a new XmlSerializer.
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);
      Group group = new Group();

      Manager manager = new Manager();
      Employee emp1 = new Employee();
      Employee emp2 = new Employee();
      manager.Name = "Consuela";
      manager.Level = 3;
      emp1.Name = "Seiko";
      emp2.Name = "Martina";
      Employee [] emps = new Employee[3]{manager, emp1, emp2};
      group.Employees = emps;

      // Creates an int and a string and assigns to ExtraInfo.
      group.ExtraInfo = new Object[3]{43, "Extra", manager};

      // Serializes the object, and closes the StreamWriter.
      s.Serialize(writer, group);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      FileStream fs = new FileStream(filename, FileMode.Open);
      XmlSerializer x = new XmlSerializer(typeof(Group));
      Group g = (Group) x.Deserialize(fs);
      Console.WriteLine("Members:");

      foreach(Employee e in g.Employees)
      {
         Console.WriteLine("\t" + e.Name);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml.Serialization

Public Class Group
    ' The XmlArrayItemAttribute allows the XmlSerializer to insert
    ' both the base type (Employee) and derived type (Manager)
    ' into serialized arrays. 
    
    <XmlArrayItem(GetType(Manager)), _
     XmlArrayItem(GetType(Employee))> _
    Public Employees() As Employee
    
    ' Use the XmlArrayItemAttribute to specify types allowed
    ' in an array of Object items. 
    <XmlArray(), _
     XmlArrayItem(GetType(Integer), ElementName := "MyNumber"), _
     XmlArrayItem(GetType(String), ElementName := "MyString"), _
     XmlArrayItem(GetType(Manager))> _
    Public ExtraInfo() As Object
End Class

Public Class Employee
    Public Name As String
End Class

Public Class Manager
    Inherits Employee
    Public Level As Integer
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("TypeDoc.xml")
        test.DeserializeObject("TypeDoc.xml")
    End Sub
    
       
    Public Sub SerializeObject(ByVal filename As String)
        ' Creates a new XmlSerializer.
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        Dim group As New Group()
        
        Dim manager As New Manager()
        Dim emp1 As New Employee()
        Dim emp2 As New Employee()
        manager.Name = "Consuela"
        manager.Level = 3
        emp1.Name = "Seiko"
        emp2.Name = "Martina"
        Dim emps() As Employee = {manager, emp1, emp2}
        group.Employees = emps
        
        ' Creates an int and a string and assigns to ExtraInfo.
        group.ExtraInfo = New Object() {43, "Extra", manager}
        
        ' Serializes the object, and closes the StreamWriter.
        s.Serialize(writer, group)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim x As New XmlSerializer(GetType(Group))
        Dim g As Group = CType(x.Deserialize(fs), Group)
        Console.WriteLine("Members:")
        
        Dim e As Employee
        For Each e In  g.Employees
            Console.WriteLine(ControlChars.Tab & e.Name)
        Next e
    End Sub
End Class

Açıklamalar

, XmlArrayItemAttribute bir nesneyi seri hale getirme veya seri durumdan çıkarma işlemini XmlSerializer denetleen bir öznitelik ailesine aittir. Benzer özniteliklerin tam listesi için bkz. XML Serileştirmesini Denetleen Öznitelikler.

öğesini bir dizi döndüren veya bir tanesine erişim sağlayan herhangi bir genel okuma/yazma üyesine uygulayabilirsiniz XmlArrayItemAttribute . Örneğin, bir nesne dizisi, bir koleksiyon, bir veya arabirimi uygulayan IEnumerable herhangi bir sınıf döndüren bir ArrayListalan.

çok XmlArrayItemAttribute biçimliliği destekler; başka bir deyişle, türetilmiş nesneleri bir diziye eklemesine olanak tanır XmlSerializer . Örneğin, adlı bir sınıfın adlı Mammal Animalbir temel sınıftan türetilmiş olduğunu varsayalım. Ayrıca adlı MyAnimals sınıfın bir nesne dizisi döndüren bir alan içerdiğini Animal varsayalım. XmlSerializer hem hem Mammal de Animal türünü seri hale getirmek için, her seferinde kabul edilebilir iki türden birini belirterek alanına iki kez uygulayınXmlArrayItemAttribute.

Not

Diziye eklenebilen XmlArrayItemAttribute nesne türlerini belirtmek için veya XmlElementAttribute öğesinin birden çok örneğini uygulayabilirsiniz.

Not

Arabirim veya arabirim dizisi döndüren bir alanın veya özelliğin seri hale getirilmesi desteklenmez.

Öznitelikleri kullanma hakkında daha fazla bilgi için bkz . Öznitelikler.

Not

sözcüğünü XmlArrayItem kodunuzda daha uzun XmlArrayItemAttributeyerine kullanabilirsiniz.

Oluşturucular

XmlArrayItemAttribute()

XmlArrayItemAttribute sınıfının yeni bir örneğini başlatır.

XmlArrayItemAttribute(String)

sınıfının yeni bir örneğini XmlArrayItemAttribute başlatır ve XML belgesinde oluşturulan XML öğesinin adını belirtir.

XmlArrayItemAttribute(String, Type)

sınıfının yeni bir örneğini XmlArrayItemAttribute başlatır ve XML belgesinde oluşturulan XML öğesinin adını ve Type oluşturulan XML belgesine eklenebilen öğesini belirtir.

XmlArrayItemAttribute(Type)

sınıfının yeni bir örneğini XmlArrayItemAttribute başlatır ve serileştirilmiş diziye eklenebilen öğesini belirtir Type .

Özellikler

DataType

Oluşturulan XML öğesinin XML veri türünü alır veya ayarlar.

ElementName

Oluşturulan XML öğesinin adını alır veya ayarlar.

Form

Oluşturulan XML öğesinin adının nitelenmiş olup olmadığını belirten bir değer alır veya ayarlar.

IsNullable

öğesinin bir üyeyiXmlSerializer, özniteliği olarak ayarlanmış trueboş bir XML etiketi olarak serileştirmesi xsi:nil gerekip gerekmediğini belirten bir değer alır veya ayarlar.

Namespace

Oluşturulan XML öğesinin ad alanını alır veya ayarlar.

NestingLevel

Etkilediği XML öğeleri XmlArrayItemAttribute hiyerarşisindeki düzeyi alır veya ayarlar.

Type

Dizide izin verilen türü alır veya ayarlar.

TypeId

Türetilmiş bir sınıfta uygulandığında, bu Attributeiçin benzersiz bir tanımlayıcı alır.

(Devralındığı yer: Attribute)

Yöntemler

Equals(Object)

Bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değeri döndürür.

(Devralındığı yer: Attribute)
GetHashCode()

Bu örneğe ilişkin karma kodu döndürür.

(Devralındığı yer: Attribute)
GetType()

Type Geçerli örneğini alır.

(Devralındığı yer: Object)
IsDefaultAttribute()

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin değerinin türetilmiş sınıf için varsayılan değer olup olmadığını gösterir.

(Devralındığı yer: Attribute)
Match(Object)

Türetilmiş bir sınıfta geçersiz kılındığında, bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değer döndürür.

(Devralındığı yer: Attribute)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli nesneyi temsil eden dizeyi döndürür.

(Devralındığı yer: Object)

Belirtik Arabirim Kullanımları

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Bir ad kümesini karşılık gelen bir dağıtma tanımlayıcısı kümesine eşler.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Bir arabirimin tür bilgilerini almak için kullanılabilecek bir nesnenin tür bilgilerini alır.

(Devralındığı yer: Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Bir nesnenin sağladığı tür bilgisi arabirimlerinin sayısını alır (0 ya da 1).

(Devralındığı yer: Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Bir nesne tarafından sunulan özelliklere ve yöntemlere erişim sağlar.

(Devralındığı yer: Attribute)

Şunlara uygulanır

Ayrıca bkz.