Bagikan melalui


XmlElementAttribute Konstruktor

Definisi

Menginisialisasi instans baru dari kelas XmlElementAttribute.

Overload

Nama Deskripsi
XmlElementAttribute()

Menginisialisasi instans baru dari kelas XmlElementAttribute.

XmlElementAttribute(String)

Menginisialisasi instans XmlElementAttribute baru kelas dan menentukan nama elemen XML.

XmlElementAttribute(Type)

Menginisialisasi instans XmlElementAttribute baru kelas dan menentukan jenis untuk anggota yang XmlElementAttribute diterapkan. Jenis ini digunakan oleh XmlSerializer ketika membuat serialisasi atau deserialisasi objek yang berisinya.

XmlElementAttribute(String, Type)

Menginisialisasi instans XmlElementAttribute baru dan menentukan nama elemen XML dan jenis turunan untuk anggota yang XmlElementAttribute diterapkan. Jenis anggota ini digunakan ketika XmlSerializer menserialisasikan objek yang berisinya.

XmlElementAttribute()

Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs

Menginisialisasi instans baru dari kelas XmlElementAttribute.

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

Contoh

Contoh berikut menerapkan ke XmlElementAttribute kelas.

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

Berlaku untuk

XmlElementAttribute(String)

Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs

Menginisialisasi instans XmlElementAttribute baru kelas dan menentukan nama elemen 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)

Parameter

elementName
String

Nama elemen XML dari anggota yang diserialisasikan.

Contoh

Contoh berikut menunjukkan kelas sederhana yang berisi satu bidang bernama Vehicles. Contoh menerapkan ke bidang dan menyertakan elementName parameter , sehingga menginstruksikan XmlElementAttributeXmlSerializer untuk menghasilkan elemen XML bernama "Mobil" daripada "Kendaraan".

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

Keterangan

Secara default, XmlSerializer menggunakan nama anggota sebagai nama elemen XML saat membuat serial instans kelas. Misalnya, bidang bernama Vehicle menghasilkan elemen XML bernama Vehicle. Namun jika Anda memerlukan elemen yang berbeda, seperti Cars, berikan dalam elementName parameter .

Berlaku untuk

XmlElementAttribute(Type)

Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs

Menginisialisasi instans XmlElementAttribute baru kelas dan menentukan jenis untuk anggota yang XmlElementAttribute diterapkan. Jenis ini digunakan oleh XmlSerializer ketika membuat serialisasi atau deserialisasi objek yang berisinya.

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)

Parameter

type
Type

Objek Type yang berasal dari jenis anggota.

Contoh

Contoh berikut menserialisasikan kelas bernama Orchestra yang berisi satu bidang bernama Instruments, yang mengembalikan array Instrument objek. Kelas kedua bernama Brass mewarisi dari Instrument kelas . Contoh menerapkan ke XmlElementAttributeInstruments bidang , dan menentukan Brass jenis , memungkinkan Instruments bidang untuk menerima Brass objek. Contoh ini juga menentukan nama elemen XML dengan mengatur ElementName properti .

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

Keterangan

type Gunakan parameter untuk menentukan jenis yang berasal dari kelas dasar. Misalnya, properti bernama MyAnimal mengembalikan Animal objek. Anda ingin meningkatkan objek, sehingga Anda membuat kelas baru bernama Mammal yang mewarisi dari Animal kelas . Untuk menginstruksikan XmlSerializer untuk menerima Mammal kelas saat menserialisasikan MyAnimal properti, teruskan TypeMammal kelas ke konstruktor.

Berlaku untuk

XmlElementAttribute(String, Type)

Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs
Sumber:
XmlElementAttribute.cs

Menginisialisasi instans XmlElementAttribute baru dan menentukan nama elemen XML dan jenis turunan untuk anggota yang XmlElementAttribute diterapkan. Jenis anggota ini digunakan ketika XmlSerializer menserialisasikan objek yang berisinya.

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)

Parameter

elementName
String

Nama elemen XML dari anggota yang diserialisasikan.

type
Type

Objek Type yang berasal dari jenis anggota.

Contoh

Contoh berikut menserialisasikan kelas bernama Orchestra yang berisi satu bidang bernama Instruments, yang mengembalikan array Instrument objek. Kelas kedua bernama Brass mewarisi dari Instrument kelas . Contoh menerapkan ke XmlElementAttributeInstruments bidang , dan menentukan Brass jenis , memungkinkan Instruments bidang untuk menerima Brass objek. Contoh ini juga menentukan nama elemen XML dengan mengatur ElementName properti .

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

Keterangan

Secara default, XmlSerializer menggunakan nama anggota sebagai nama elemen XML saat membuat serial instans kelas. Misalnya, bidang bernama Vehicle menghasilkan elemen XML bernama Vehicle. Namun, jika Anda memerlukan elemen yang berbeda, seperti Cars, berikan dalam elementName parameter .

type Gunakan parameter untuk menentukan jenis yang berasal dari kelas dasar. Misalnya, properti bernama MyAnimal mengembalikan Animal objek. Anda ingin meningkatkan objek, sehingga Anda membuat kelas baru bernama Mammal yang mewarisi dari Animal kelas . Untuk menginstruksikan XmlSerializer untuk menerima Mammal kelas saat menserialisasikan MyAnimal properti, teruskan TypeMammal kelas ke konstruktor.

Berlaku untuk