Aracılığıyla paylaş


XmlElementAttribute Oluşturucular

Tanım

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

Aşırı Yüklemeler

Name Description
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 nesne seri hale getirilirken veya seri durumdan çıkarılı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()

Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs

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 class MyClass
{
   [XmlElement()]
   public string TeacherName;
}
Public Class MyClass1
    <XmlElement()> Public TeacherName As String
End Class

Şunlara uygulanır

XmlElementAttribute(String)

Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs

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

Serileştirilmiş üyenin XML öğesi adı.

Örnekler

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

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 bir sınıf örneğini seri hale getirdiğinizde 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 parametresine elementName geçirin.

Şunlara uygulanır

XmlElementAttribute(Type)

Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs

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 nesne seri hale getirilirken veya seri durumdan çıkarılı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 nesnenin değeri.

Örnekler

Aşağıdaki örnek, adlı tek bir alan Instrumentsiçeren ve Orchestra bir nesne dizisi Instrument döndüren bir sınıfı serileştirir. adlı Brass ikinci bir sınıf sınıfından devralır Instrument . Örnek, alanını uygular XmlElementAttributeInstruments ve türü belirterek BrassInstruments alanın nesneleri kabul Brass etmesine olanak sağlar. Örnek ayrıca özelliğini ayarlayarak XML öğesinin ElementName adını belirtir.

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üğü varsayalım. 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 XmlSerializerMyAnimal vermek için sınıfının öğesini oluşturucuya geçirin TypeMammal.Mammal

Şunlara uygulanır

XmlElementAttribute(String, Type)

Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs
Kaynak:
XmlElementAttribute.cs

öğ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

Serileştirilmiş üyenin XML öğesi adı.

type
Type

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

Örnekler

Aşağıdaki örnek, adlı tek bir alan Instrumentsiçeren ve Orchestra bir nesne dizisi Instrument döndüren bir sınıfı serileştirir. adlı Brass ikinci bir sınıf sınıfından devralır Instrument . Örnek, alanını uygular XmlElementAttributeInstruments ve türü belirterek BrassInstruments alanın nesneleri kabul Brass etmesine olanak sağlar. Örnek ayrıca özelliğini ayarlayarak XML öğesinin ElementName adını belirtir.

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 bir sınıf örneğini seri hale getirdiğinizde 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 parametresine 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üğü varsayalım. 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 XmlSerializerMyAnimal vermek için sınıfının öğesini oluşturucuya geçirin TypeMammal.Mammal

Şunlara uygulanır