Udostępnij za pośrednictwem


XmlElementAttribute Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy XmlElementAttribute.

Przeciążenia

Nazwa Opis
XmlElementAttribute()

Inicjuje nowe wystąpienie klasy XmlElementAttribute.

XmlElementAttribute(String)

Inicjuje nowe wystąpienie XmlElementAttribute klasy i określa nazwę elementu XML.

XmlElementAttribute(Type)

Inicjuje nowe wystąpienie XmlElementAttribute klasy i określa typ elementu członkowskiego, do którego XmlElementAttribute jest stosowany. Ten typ jest używany przez XmlSerializer obiekt podczas serializacji lub deserializacji obiektu, który go zawiera.

XmlElementAttribute(String, Type)

Inicjuje nowe wystąpienie XmlElementAttribute elementu i określa nazwę elementu XML i typ pochodny dla elementu członkowskiego, do którego XmlElementAttribute jest stosowany. Ten typ elementu członkowskiego jest używany, gdy XmlSerializer serializuje obiekt, który go zawiera.

XmlElementAttribute()

Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs

Inicjuje nowe wystąpienie klasy XmlElementAttribute.

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

Przykłady

Poniższy przykład dotyczy XmlElementAttribute klasy .

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

Dotyczy

XmlElementAttribute(String)

Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs

Inicjuje nowe wystąpienie XmlElementAttribute klasy i określa nazwę elementu 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)

Parametry

elementName
String

Nazwa elementu XML elementu serializowanego elementu członkowskiego.

Przykłady

W poniższym przykładzie przedstawiono prostą klasę zawierającą jedno pole o nazwie Vehicles. Przykład dotyczy XmlElementAttribute pola i zawiera elementName parametr , a tym samym instruując XmlSerializer element XML o nazwie "Samochody", a nie "Pojazdy".

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

Uwagi

Domyślnie XmlSerializer nazwa elementu członkowskiego jest używana jako nazwa elementu XML podczas serializacji wystąpienia klasy. Na przykład pole o nazwie Vehicle generuje element XML o nazwie Vehicle. Jeśli jednak potrzebujesz innego elementu, takiego jak Cars, przekaż go do parametru elementName .

Dotyczy

XmlElementAttribute(Type)

Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs

Inicjuje nowe wystąpienie XmlElementAttribute klasy i określa typ elementu członkowskiego, do którego XmlElementAttribute jest stosowany. Ten typ jest używany przez XmlSerializer obiekt podczas serializacji lub deserializacji obiektu, który go zawiera.

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)

Parametry

type
Type

Obiekt Type pochodzący z typu elementu członkowskiego.

Przykłady

Poniższy przykład serializuje klasę o nazwie , która zawiera jedno pole o nazwie OrchestraInstruments, które zwraca tablicę Instrument obiektów. Druga klasa o nazwie Brass dziedziczy z Instrument klasy . Przykład dotyczy XmlElementAttributeInstruments pola i określa Brass typ, który umożliwia akceptowanie InstrumentsBrass obiektów przez pole. Przykład określa również nazwę elementu XML, ustawiając ElementName właściwość .

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

Uwagi

Użyj parametru , type aby określić typ, który pochodzi z klasy bazowej. Załóżmy na przykład, że właściwość o nazwie MyAnimal zwraca Animal obiekt. Chcesz ulepszyć obiekt, aby utworzyć nową klasę o nazwie Mammal dziedziczonej Animal z klasy. Aby poinstruować XmlSerializer klasę, aby akceptowała klasę Mammal podczas serializacji MyAnimal właściwości, przekaż TypeMammal klasę do konstruktora.

Dotyczy

XmlElementAttribute(String, Type)

Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs
Źródło:
XmlElementAttribute.cs

Inicjuje nowe wystąpienie XmlElementAttribute elementu i określa nazwę elementu XML i typ pochodny dla elementu członkowskiego, do którego XmlElementAttribute jest stosowany. Ten typ elementu członkowskiego jest używany, gdy XmlSerializer serializuje obiekt, który go zawiera.

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)

Parametry

elementName
String

Nazwa elementu XML elementu serializowanego elementu członkowskiego.

type
Type

Obiekt Type pochodzący z typu elementu członkowskiego.

Przykłady

Poniższy przykład serializuje klasę o nazwie , która zawiera jedno pole o nazwie OrchestraInstruments, które zwraca tablicę Instrument obiektów. Druga klasa o nazwie Brass dziedziczy z Instrument klasy . Przykład dotyczy XmlElementAttributeInstruments pola i określa Brass typ, który umożliwia akceptowanie InstrumentsBrass obiektów przez pole. Przykład określa również nazwę elementu XML, ustawiając ElementName właściwość .

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

Uwagi

Domyślnie XmlSerializer nazwa elementu członkowskiego jest używana jako nazwa elementu XML podczas serializacji wystąpienia klasy. Na przykład pole o nazwie Vehicle generuje element XML o nazwie Vehicle. Jeśli jednak potrzebujesz innego elementu, takiego jak Cars, przekaż go w parametrze elementName .

Użyj parametru , type aby określić typ, który pochodzi z klasy bazowej. Załóżmy na przykład, że właściwość o nazwie MyAnimal zwraca Animal obiekt. Chcesz ulepszyć obiekt, aby utworzyć nową klasę o nazwie Mammal dziedziczonej Animal z klasy. Aby poinstruować XmlSerializer klasę, aby akceptowała klasę Mammal podczas serializacji MyAnimal właściwości, przekaż TypeMammal klasę do konstruktora.

Dotyczy