XmlElementAttribute Konstruktoren

Definition

Initialisiert eine neue Instanz der XmlElementAttribute-Klasse.Initializes a new instance of the XmlElementAttribute class.

Überlädt

XmlElementAttribute()

Initialisiert eine neue Instanz der XmlElementAttribute-Klasse.Initializes a new instance of the XmlElementAttribute class.

XmlElementAttribute(String)

Initialisiert eine neue Instanz der XmlElementAttribute-Klasse und gibt den Namen des XML-Elements an.Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.

XmlElementAttribute(Type)

Initialisiert eine neues Instanz der XmlElementAttribute-Klasse und gibt einen Typ für den Member an, auf den das XmlElementAttribute angewendet wird.Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. Dieser Typ wird vom XmlSerializer verwendet, wenn das Objekt serialisiert oder deserialisiert wird, in dem es enthalten ist.This type is used by the XmlSerializer when serializing or deserializing object that contains it.

XmlElementAttribute(String, Type)

Initialisiert eine neue Instanz von XmlElementAttribute, und gibt den Namen des XML-Elements und einen abgeleiteten Typ für den Member an, auf den das XmlElementAttribute angewendet wird.Initializes a new instance of the XmlElementAttribute and specifies the name of the XML element and a derived type for the member to which the XmlElementAttribute is applied. Dieser Membertyp wird verwendet, wenn der XmlSerializer das Objekt serialisiert, in dem es enthalten ist.This member type is used when the XmlSerializer serializes the object that contains it.

XmlElementAttribute()

Initialisiert eine neue Instanz der XmlElementAttribute-Klasse.Initializes a new instance of the XmlElementAttribute class.

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

Beispiele

Im folgenden Beispiel wird der XmlElementAttribute auf eine Klasse angewendet.The following example applies the XmlElementAttribute to a class.

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

XmlElementAttribute(String)

Initialisiert eine neue Instanz der XmlElementAttribute-Klasse und gibt den Namen des XML-Elements an.Initializes a new instance of the XmlElementAttribute class and specifies the name of the XML element.

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

Parameter

elementName
String

Der XML-Elementname des serialisierten Members.The XML element name of the serialized member.

Beispiele

Das folgende Beispiel zeigt eine einfache Klasse, die ein einzelnes Feld mit Vehiclesdem Namen enthält.The following example shows a simple class that contains a single field named Vehicles. Im Beispiel wird das XmlElementAttribute -Element auf das-Feld elementName angewendet und der-Parameter eingeschlossen XmlSerializer . Dadurch wird der angewiesen, XML-Elemente mit dem Namen "Cars" anstelle von "Vehicles" zu generieren.The example applies the XmlElementAttribute to the field and includes the elementName parameter, thereby instructing the XmlSerializer to generate XML elements named "Cars" rather than "Vehicles".

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

Hinweise

Standardmäßig verwendet den XmlSerializer Elementnamen als XML-Elementnamen beim Serialisieren einer Klasseninstanz.By default, the XmlSerializer uses the member name as the XML element name when serializing a class instance. Ein-Feld mit dem Namen Vehicle generiert beispielsweise ein XML Vehicle-Element mit dem Namen.For example, a field named Vehicle generates an XML element named Vehicle. Wenn Sie jedoch ein anderes Element benötigen, z Cars. b., übergeben Sie es elementName im-Parameter.However if you need a different element, such as Cars, pass it in the elementName parameter.

XmlElementAttribute(Type)

Initialisiert eine neues Instanz der XmlElementAttribute-Klasse und gibt einen Typ für den Member an, auf den das XmlElementAttribute angewendet wird.Initializes a new instance of the XmlElementAttribute class and specifies a type for the member to which the XmlElementAttribute is applied. Dieser Typ wird vom XmlSerializer verwendet, wenn das Objekt serialisiert oder deserialisiert wird, in dem es enthalten ist.This type is used by the XmlSerializer when serializing or deserializing object that contains it.

public:
 XmlElementAttribute(Type ^ type);
public XmlElementAttribute (Type type);
new System.Xml.Serialization.XmlElementAttribute : Type -> System.Xml.Serialization.XmlElementAttribute

Parameter

type
Type

Der Type eines Objekts, das vom Typ des Members abgeleitet ist.The Type of an object derived from the member's type.

Beispiele

Im folgenden Beispiel wird eine Klasse mit dem Orchestra Namen serialisiert, die ein Instrumentseinzelnes Feld mit dem Namen enthält Instrument , das ein Array von-Objekten zurückgibt.The following example serializes a class named Orchestra that contains a single field named Instruments, which returns an array of Instrument objects. Eine zweite Klasse mit Brass dem Namen erbt Instrument von der-Klasse.A second class named Brass inherits from the Instrument class. Im Beispiel wird das XmlElementAttribute auf das Instruments -Feld angewendet, und Brass es wird der- Instruments Typ angegeben, Brass sodass das Feld-Objekte akzeptieren kann.The example applies the XmlElementAttribute to the Instruments field, and specifies the Brass type, allowing the Instruments field to accept Brass objects. Im Beispiel wird auch der Name des XML-Elements angegeben, indem ElementName die-Eigenschaft festgelegt wird.The example also specifies the name of the XML element by setting the ElementName property.

#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

Hinweise

Verwenden Sie type den-Parameter, um einen Typ anzugeben, der von einer Basisklasse abgeleitet ist.Use the type parameter to specify a type that is derived from a base class. Angenommen, eine Eigenschaft mit dem Namen MyAnimal gibt ein Animal -Objekt zurück.For example, suppose a property named MyAnimal returns an Animal object. Wenn Sie das Objekt erweitern möchten, erstellen Sie eine neue Klasse mit dem Mammal Namen, die von der Animal -Klasse erbt.You want to enhance the object, so you create a new class named Mammal that inherits from the Animal class. XmlSerializer Um den anzuweisen, die Mammal -Klasse beim Serialisieren der MyAnimal -Eigenschaft zu akzeptieren, übergeben Mammal Sie den Type der-Klasse an den-Konstruktor.To instruct the XmlSerializer to accept the Mammal class when it serializes the MyAnimal property, pass the Type of the Mammal class to the constructor.

XmlElementAttribute(String, Type)

Initialisiert eine neue Instanz von XmlElementAttribute, und gibt den Namen des XML-Elements und einen abgeleiteten Typ für den Member an, auf den das XmlElementAttribute angewendet wird.Initializes a new instance of the XmlElementAttribute and specifies the name of the XML element and a derived type for the member to which the XmlElementAttribute is applied. Dieser Membertyp wird verwendet, wenn der XmlSerializer das Objekt serialisiert, in dem es enthalten ist.This member type is used when the XmlSerializer serializes the object that contains it.

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

Parameter

elementName
String

Der XML-Elementname des serialisierten Members.The XML element name of the serialized member.

type
Type

Der Type eines Objekts, das vom Typ des Members abgeleitet ist.The Type of an object derived from the member's type.

Beispiele

Im folgenden Beispiel wird eine Klasse mit dem Orchestra Namen serialisiert, die ein Instrumentseinzelnes Feld mit dem Namen enthält Instrument , das ein Array von-Objekten zurückgibt.The following example serializes a class named Orchestra that contains a single field named Instruments, which returns an array of Instrument objects. Eine zweite Klasse mit Brass dem Namen erbt Instrument von der-Klasse.A second class named Brass inherits from the Instrument class. Im Beispiel wird das XmlElementAttribute auf das Instruments -Feld angewendet, und Brass es wird der- Instruments Typ angegeben, Brass sodass das Feld-Objekte akzeptieren kann.The example applies the XmlElementAttribute to the Instruments field, and specifies the Brass type, allowing the Instruments field to accept Brass objects. Im Beispiel wird auch der Name des XML-Elements angegeben, indem ElementName die-Eigenschaft festgelegt wird.The example also specifies the name of the XML element by setting the ElementName property.

#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

Hinweise

Standardmäßig verwendet den XmlSerializer Elementnamen als XML-Elementnamen beim Serialisieren einer Klasseninstanz.By default, the XmlSerializer uses the member name as the XML element name when serializing a class instance. Ein-Feld mit dem Namen Vehicle generiert beispielsweise ein XML Vehicle-Element mit dem Namen.For example, a field named Vehicle generates an XML element named Vehicle. Wenn Sie jedoch ein anderes Element benötigen, z Cars. b., übergeben Sie es elementName im-Parameter.However, if you need a different element, such as Cars, pass it in the elementName parameter.

Verwenden Sie type den-Parameter, um einen Typ anzugeben, der von einer Basisklasse abgeleitet ist.Use the type parameter to specify a type that is derived from a base class. Angenommen, eine Eigenschaft mit dem Namen MyAnimal gibt ein Animal -Objekt zurück.For example, suppose a property named MyAnimal returns an Animal object. Wenn Sie das Objekt erweitern möchten, erstellen Sie eine neue Klasse mit dem Mammal Namen, die von der Animal -Klasse erbt.You want to enhance the object, so you create a new class named Mammal that inherits from the Animal class. XmlSerializer Um den anzuweisen, die Mammal -Klasse beim Serialisieren der MyAnimal -Eigenschaft zu akzeptieren, übergeben Mammal Sie den Type der-Klasse an den-Konstruktor.To instruct the XmlSerializer to accept the Mammal class when it serializes the MyAnimal property, pass the Type of the Mammal class to the constructor.

Gilt für: