XmlElementAttribute Constructeurs

Définition

Initialise une nouvelle instance de la classe XmlElementAttribute.

Surcharges

XmlElementAttribute()

Initialise une nouvelle instance de la classe XmlElementAttribute.

XmlElementAttribute(String)

Initialise une nouvelle instance de la classe XmlElementAttribute et spécifie le nom de l'élément XML.

XmlElementAttribute(Type)

Initialise une nouvelle instance de la classe XmlElementAttribute et spécifie un type pour le membre auquel XmlElementAttribute est appliqué. Ce type est utilisé par XmlSerializer lors de la sérialisation ou la désérialisation de l'objet qui le contient.

XmlElementAttribute(String, Type)

Initialise une nouvelle instance de XmlElementAttribute et spécifie le nom de l'élément XML et un type dérivé pour le membre auquel XmlElementAttribute est appliqué. Ce type de membre est utilisé lorsque XmlSerializer sérialise l'objet qui le contient.

XmlElementAttribute()

Initialise une nouvelle instance de la classe XmlElementAttribute.

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

Exemples

L’exemple suivant applique la XmlElementAttribute classe.

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

S’applique à

XmlElementAttribute(String)

Initialise une nouvelle instance de la classe XmlElementAttribute et spécifie le nom de l'élément XML.

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)

Paramètres

elementName
String

Nom de l'élément XML du membre sérialisé.

Exemples

L’exemple suivant montre une classe simple qui contient un seul champ nommé Vehicles. L’exemple applique le XmlElementAttribute champ et inclut le elementName paramètre, en demandant ainsi de générer des XmlSerializer éléments XML nommés « Cars » plutôt que « Véhicules ».

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

Remarques

Par défaut, le XmlSerializer nom du membre est utilisé comme nom d’élément XML lors de la sérialisation d’une instance de classe. Par exemple, un champ nommé Vehicle génère un élément XML nommé Vehicle. Toutefois, si vous avez besoin d’un autre élément, tel que Cars, passez-le dans le elementName paramètre.

S’applique à

XmlElementAttribute(Type)

Initialise une nouvelle instance de la classe XmlElementAttribute et spécifie un type pour le membre auquel XmlElementAttribute est appliqué. Ce type est utilisé par XmlSerializer lors de la sérialisation ou la désérialisation de l'objet qui le contient.

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)

Paramètres

type
Type

Type d'un objet dérivé du type du membre.

Exemples

L’exemple suivant sérialise une classe nommée Orchestra qui contient un seul champ nommé Instruments, qui retourne un tableau d’objets Instrument . Une deuxième classe nommée Brass hérite de la Instrument classe. L’exemple applique le XmlElementAttribute Instruments champ et spécifie le Brass type, ce qui permet Instruments au champ d’accepter des Brass objets. L’exemple spécifie également le nom de l’élément XML en définissant la ElementName propriété.

#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

Remarques

Utilisez le type paramètre pour spécifier un type dérivé d’une classe de base. Par exemple, supposons qu’une propriété nommée MyAnimal retourne un Animal objet. Vous souhaitez améliorer l’objet, de sorte que vous créez une classe nommée Mammal qui hérite de la Animal classe. Pour indiquer à la XmlSerializer Mammal classe d’accepter la classe lorsqu’elle sérialise la MyAnimal propriété, transmettez la Type Mammal classe au constructeur.

S’applique à

XmlElementAttribute(String, Type)

Initialise une nouvelle instance de XmlElementAttribute et spécifie le nom de l'élément XML et un type dérivé pour le membre auquel XmlElementAttribute est appliqué. Ce type de membre est utilisé lorsque XmlSerializer sérialise l'objet qui le contient.

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)

Paramètres

elementName
String

Nom de l'élément XML du membre sérialisé.

type
Type

Type d'un objet dérivé du type du membre.

Exemples

L’exemple suivant sérialise une classe nommée Orchestra qui contient un seul champ nommé Instruments, qui retourne un tableau d’objets Instrument . Une deuxième classe nommée Brass hérite de la Instrument classe. L’exemple applique le XmlElementAttribute Instruments champ et spécifie le Brass type, ce qui permet Instruments au champ d’accepter des Brass objets. L’exemple spécifie également le nom de l’élément XML en définissant la ElementName propriété.

#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

Remarques

Par défaut, le XmlSerializer nom du membre est utilisé comme nom d’élément XML lors de la sérialisation d’une instance de classe. Par exemple, un champ nommé Vehicle génère un élément XML nommé Vehicle. Toutefois, si vous avez besoin d’un autre élément, tel que Cars, passez-le dans le elementName paramètre.

Utilisez le type paramètre pour spécifier un type dérivé d’une classe de base. Par exemple, supposons qu’une propriété nommée MyAnimal retourne un Animal objet. Vous souhaitez améliorer l’objet, de sorte que vous créez une classe nommée Mammal qui hérite de la Animal classe. Pour indiquer à la XmlSerializer Mammal classe d’accepter la classe lorsqu’elle sérialise la MyAnimal propriété, transmettez la Type Mammal classe au constructeur.

S’applique à