XmlElementAttribute Oluşturucular

Tanım

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

Aşırı Yüklemeler

XmlElementAttribute()

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

XmlElementAttribute(String)

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

XmlElementAttribute(Type)

sınıfının yeni bir örneğini XmlElementAttribute başlatır ve öğesinin uygulandığı üye XmlElementAttribute için bir tür belirtir. Bu tür, onu içeren nesneyi serileştirirken veya seri durumdan çıkarırken tarafından XmlSerializer kullanılır.

XmlElementAttribute(String, Type)

öğesinin XmlElementAttribute yeni bir örneğini başlatır ve XML öğesinin adını ve uygulandığı üye XmlElementAttribute için türetilmiş bir türü belirtir. Bu üye türü, onu içeren nesneyi seri hale getirdiğinde XmlSerializer kullanılır.

XmlElementAttribute()

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

public:
 XmlElementAttribute();
public XmlElementAttribute ();
Public Sub New ()

Örnekler

Aşağıdaki örnek öğesini XmlElementAttribute bir sınıfa uygular.

public ref class MyClass
{
public:

   [XmlElement]
   String^ TeacherName;
};
public class MyClass
{
   [XmlElement()]
   public string TeacherName;
}
Public Class MyClass1
    <XmlElement()> Public TeacherName As String
End Class

Şunlara uygulanır

XmlElementAttribute(String)

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

public:
 XmlElementAttribute(System::String ^ elementName);
public XmlElementAttribute (string elementName);
public XmlElementAttribute (string? elementName);
new System.Xml.Serialization.XmlElementAttribute : string -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (elementName As String)

Parametreler

elementName
String

Seri hale getirilmiş üyenin XML öğesi adı.

Örnekler

Aşağıdaki örnekte adlı Vehiclestek bir alan içeren basit bir sınıf gösterilmektedir. Örnek, alanını uygular XmlElementAttribute ve parametresini elementName içerir ve bu sayede öğesine "Araçlar" yerine "Arabalar" adlı XML öğeleri oluşturma talimatını XmlSerializer vemektedir.

public ref class Transportation
{
public:

   [XmlElement("Cars")]
   String^ Vehicles;
};
public class Transportation
{
   [XmlElement("Cars")]
   public string Vehicles;
}
Public Class Transportation
    <XmlElement("Cars")> Public Vehicles As String
End Class

Açıklamalar

Varsayılan olarak, XmlSerializer sınıf örneği seri hale getirildiğinde XML öğesi adı olarak üye adını kullanır. Örneğin, adlı Vehicle bir alan adlı Vehiclebir XML öğesi oluşturur. Ancak gibi Carsfarklı bir öğeye ihtiyacınız varsa parametresinde elementName geçirin.

Şunlara uygulanır

XmlElementAttribute(Type)

sınıfının yeni bir örneğini XmlElementAttribute başlatır ve öğesinin uygulandığı üye XmlElementAttribute için bir tür belirtir. Bu tür, onu içeren nesneyi serileştirirken veya seri durumdan çıkarırken tarafından XmlSerializer kullanılır.

public:
 XmlElementAttribute(Type ^ type);
public XmlElementAttribute (Type type);
public XmlElementAttribute (Type? type);
new System.Xml.Serialization.XmlElementAttribute : Type -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (type As Type)

Parametreler

type
Type

Type Üyenin türünden türetilen bir nesnenin değeri.

Örnekler

Aşağıdaki örnek, bir nesne dizisi Instrument döndüren adlı tek bir alan içeren adlı Orchestra Instrumentssınıfı seri hale getirmektedir. adlı Brass ikinci sınıf sınıfından devralır Instrument . Örnek, alanını alana uygular XmlElementAttribute Instruments ve türü belirtir Brass ve alanın nesneleri kabul Brass etmesine izin verirInstruments. Örnek ayrıca özelliğini ayarlayarak XML öğesinin ElementName adını belirtir.

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Instrument
{
public:
   String^ Name;
};

public ref class Brass: public Instrument
{
public:
   bool IsValved;
};

public ref class Orchestra
{
public:
   array<Instrument^>^Instruments;
};

void SerializeObject( String^ filename )
{
   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that overrides the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );

   // Creates the object to serialize.
   Orchestra^ band = gcnew Orchestra;

   // Creates an object of the derived type.
   Brass^ i = gcnew Brass;
   i->Name = "Trumpet";
   i->IsValved = true;
   array<Instrument^>^myInstruments = {i};
   band->Instruments = myInstruments;
   s->Serialize( writer, band );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that override the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs ));
   Console::WriteLine( "Brass:" );

   /* Deserializing differs from serializing. To read the 
      derived-object values, declare an object of the derived 
      type (Brass) and cast the Instrument instance to it. */
   Brass^ b;
   System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Instrument^ i = safe_cast<Instrument^>(myEnum->Current);
      b = dynamic_cast<Brass^>(i);
      Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved );
   }
}

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

public class Orchestra
{
   public Instrument[] Instruments;
}

public class Instrument
{
   public string Name;
}

public class Brass:Instrument{
   public bool IsValved;
}

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

    public void SerializeObject(string filename)
    {
      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);

      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that overrides the Instrument type.
      XmlElementAttribute attr = new
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      // Creates the object to serialize.
      Orchestra band = new Orchestra();

      // Creates an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that override the Instrument type.
      XmlElementAttribute attr = new
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* Deserializing differs from serializing. To read the
         derived-object values, declare an object of the derived
         type (Brass) and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments)
      {
         b= (Brass)i;
         Console.WriteLine(
         b.Name + "\n" +
         b.IsValved);
      }
   }
}
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml.Serialization

Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub
    
    
    Public Sub SerializeObject(filename As String)
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Creates an XmlElementAttribute that overrides the Instrument type.
        Dim attr As New XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Adds the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Creates the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        ' Creates the object to serialize.
        Dim band As New Orchestra()
        
        ' Creates an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument = {i}
        band.Instruments = myInstruments
        s.Serialize(writer, band)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(filename As String)
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute that override the Instrument type.
        Dim attr As New XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra)
        Console.WriteLine("Brass:")
        
        ' Deserializing differs from serializing. To read the
        ' derived-object values, declare an object of the derived
        ' type (Brass) and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString()))
        Next i
    End Sub
End Class

Açıklamalar

Bir temel sınıftan type türetilen bir tür belirtmek için parametresini kullanın. Örneğin, adlı MyAnimal bir özelliğin bir Animal nesnesi döndürdüğü varsaydır. nesnesini geliştirmek istiyorsunuz, böylece sınıfından Animal devralan adlı Mammal yeni bir sınıf oluşturacaksınız. özelliğini serileştirdiğinde sınıfını kabul etme yönergesi XmlSerializer MyAnimal vermek için sınıfının öğesini oluşturucuya geçirin Type Mammal.Mammal

Şunlara uygulanır

XmlElementAttribute(String, Type)

öğesinin XmlElementAttribute yeni bir örneğini başlatır ve XML öğesinin adını ve uygulandığı üye XmlElementAttribute için türetilmiş bir türü belirtir. Bu üye türü, onu içeren nesneyi seri hale getirdiğinde XmlSerializer kullanılır.

public:
 XmlElementAttribute(System::String ^ elementName, Type ^ type);
public XmlElementAttribute (string elementName, Type type);
public XmlElementAttribute (string? elementName, Type? type);
new System.Xml.Serialization.XmlElementAttribute : string * Type -> System.Xml.Serialization.XmlElementAttribute
Public Sub New (elementName As String, type As Type)

Parametreler

elementName
String

Seri hale getirilmiş üyenin XML öğesi adı.

type
Type

Type Üyenin türünden türetilen bir nesnenin değeri.

Örnekler

Aşağıdaki örnek, bir nesne dizisi Instrument döndüren adlı tek bir alan içeren adlı Orchestra Instrumentssınıfı seri hale getirmektedir. adlı Brass ikinci sınıf sınıfından devralır Instrument . Örnek, alanını alana uygular XmlElementAttribute Instruments ve türü belirtir Brass ve alanın nesneleri kabul Brass etmesine izin verirInstruments. Örnek ayrıca özelliğini ayarlayarak XML öğesinin ElementName adını belirtir.

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Instrument
{
public:
   String^ Name;
};

public ref class Brass: public Instrument
{
public:
   bool IsValved;
};

public ref class Orchestra
{
public:
   array<Instrument^>^Instruments;
};

void SerializeObject( String^ filename )
{
   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that overrides the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );

   // Creates the object to serialize.
   Orchestra^ band = gcnew Orchestra;

   // Creates an object of the derived type.
   Brass^ i = gcnew Brass;
   i->Name = "Trumpet";
   i->IsValved = true;
   array<Instrument^>^myInstruments = {i};
   band->Instruments = myInstruments;
   s->Serialize( writer, band );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Creates an XmlElementAttribute that override the Instrument type.
   XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
   attr->ElementName = "Brass";

   // Adds the element to the collection of elements.
   attrs->XmlElements->Add( attr );
   attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );

   // Creates the XmlSerializer using the XmlAttributeOverrides.
   XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs ));
   Console::WriteLine( "Brass:" );

   /* Deserializing differs from serializing. To read the 
      derived-object values, declare an object of the derived 
      type (Brass) and cast the Instrument instance to it. */
   Brass^ b;
   System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Instrument^ i = safe_cast<Instrument^>(myEnum->Current);
      b = dynamic_cast<Brass^>(i);
      Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved );
   }
}

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

public class Orchestra
{
   public Instrument[] Instruments;
}

public class Instrument
{
   public string Name;
}

public class Brass:Instrument{
   public bool IsValved;
}

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

    public void SerializeObject(string filename)
    {
      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);

      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that overrides the Instrument type.
      XmlElementAttribute attr = new
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      // Creates the object to serialize.
      Orchestra band = new Orchestra();

      // Creates an object of the derived type.
      Brass i = new Brass();
      i.Name = "Trumpet";
      i.IsValved = true;
      Instrument[] myInstruments = {i};
      band.Instruments = myInstruments;
      s.Serialize(writer,band);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlAttributeOverrides attrOverrides =
         new XmlAttributeOverrides();
      XmlAttributes attrs = new XmlAttributes();

      // Creates an XmlElementAttribute that override the Instrument type.
      XmlElementAttribute attr = new
      XmlElementAttribute(typeof(Brass));
      attr.ElementName = "Brass";

      // Adds the element to the collection of elements.
      attrs.XmlElements.Add(attr);
      attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);

      // Creates the XmlSerializer using the XmlAttributeOverrides.
      XmlSerializer s =
      new XmlSerializer(typeof(Orchestra), attrOverrides);

      FileStream fs = new FileStream(filename, FileMode.Open);
      Orchestra band = (Orchestra) s.Deserialize(fs);
      Console.WriteLine("Brass:");

      /* Deserializing differs from serializing. To read the
         derived-object values, declare an object of the derived
         type (Brass) and cast the Instrument instance to it. */
      Brass b;
      foreach(Instrument i in band.Instruments)
      {
         b= (Brass)i;
         Console.WriteLine(
         b.Name + "\n" +
         b.IsValved);
      }
   }
}
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml.Serialization

Public Class Orchestra
    Public Instruments() As Instrument
End Class

Public Class Instrument
    Public Name As String
End Class

Public Class Brass
    Inherits Instrument
    Public IsValved As Boolean
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Override.xml")
        test.DeserializeObject("Override.xml")
    End Sub
    
    
    Public Sub SerializeObject(filename As String)
        ' To write the file, a TextWriter is required.
        Dim writer As New StreamWriter(filename)
        
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Creates an XmlElementAttribute that overrides the Instrument type.
        Dim attr As New XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Adds the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Creates the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        ' Creates the object to serialize.
        Dim band As New Orchestra()
        
        ' Creates an object of the derived type.
        Dim i As New Brass()
        i.Name = "Trumpet"
        i.IsValved = True
        Dim myInstruments() As Instrument = {i}
        band.Instruments = myInstruments
        s.Serialize(writer, band)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(filename As String)
        Dim attrOverrides As New XmlAttributeOverrides()
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlElementAttribute that override the Instrument type.
        Dim attr As New XmlElementAttribute(GetType(Brass))
        attr.ElementName = "Brass"
        
        ' Add the element to the collection of elements.
        attrs.XmlElements.Add(attr)
        attrOverrides.Add(GetType(Orchestra), "Instruments", attrs)
        
        ' Create the XmlSerializer using the XmlAttributeOverrides.
        Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides)
        
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra)
        Console.WriteLine("Brass:")
        
        ' Deserializing differs from serializing. To read the
        ' derived-object values, declare an object of the derived
        ' type (Brass) and cast the Instrument instance to it. 
        Dim b As Brass
        Dim i As Instrument
        For Each i In  band.Instruments
            b = CType(i, Brass)
            Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString()))
        Next i
    End Sub
End Class

Açıklamalar

Varsayılan olarak, XmlSerializer sınıf örneği seri hale getirildiğinde XML öğesi adı olarak üye adını kullanır. Örneğin, adlı Vehicle bir alan adlı Vehiclebir XML öğesi oluşturur. Ancak, gibi Carsfarklı bir öğeye ihtiyacınız varsa parametresinde elementName geçirin.

Bir temel sınıftan type türetilen bir tür belirtmek için parametresini kullanın. Örneğin, adlı MyAnimal bir özelliğin bir Animal nesnesi döndürdüğü varsaydır. nesnesini geliştirmek istiyorsunuz, böylece sınıfından Animal devralan adlı Mammal yeni bir sınıf oluşturacaksınız. özelliğini serileştirdiğinde sınıfını kabul etme yönergesi XmlSerializer MyAnimal vermek için sınıfının öğesini oluşturucuya geçirin Type Mammal.Mammal

Şunlara uygulanır