XmlElementAttribute Konstruktorer

Definition

Initierar en ny instans av XmlElementAttribute klassen.

Överlagringar

Name Description
XmlElementAttribute()

Initierar en ny instans av XmlElementAttribute klassen.

XmlElementAttribute(String)

Initierar en ny instans av XmlElementAttribute klassen och anger namnet på XML-elementet.

XmlElementAttribute(Type)

Initierar en ny instans av XmlElementAttribute klassen och anger en typ för den medlem som XmlElementAttribute tillämpas på. Den här typen används av objektet XmlSerializer vid serialisering eller deserialisering som innehåller det.

XmlElementAttribute(String, Type)

Initierar en ny instans av XmlElementAttribute och anger namnet på XML-elementet och en härledd typ för medlemmen som XmlElementAttribute används. Den här medlemstypen används när XmlSerializer serialiserar objektet som innehåller det.

XmlElementAttribute()

Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs

Initierar en ny instans av XmlElementAttribute klassen.

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

Exempel

Följande exempel gäller XmlElementAttribute för en klass.

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

Gäller för

XmlElementAttribute(String)

Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs

Initierar en ny instans av XmlElementAttribute klassen och anger namnet på XML-elementet.

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)

Parametrar

elementName
String

XML-elementnamnet för den serialiserade medlemmen.

Exempel

I följande exempel visas en enkel klass som innehåller ett enda fält med namnet Vehicles. Exemplet gäller XmlElementAttribute för fältet och innehåller parametern elementName , vilket instruerar XmlSerializer att generera XML-element med namnet "Bilar" i stället för "Fordon".

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

Kommentarer

Som standard XmlSerializer använder medlemsnamnet som XML-elementnamn när du serialiserar en klassinstans. Ett fält med namnet Vehicle genererar till exempel ett XML-element med namnet Vehicle. Men om du behöver ett annat element, till exempel Cars, skickar du det i parametern elementName .

Gäller för

XmlElementAttribute(Type)

Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs

Initierar en ny instans av XmlElementAttribute klassen och anger en typ för den medlem som XmlElementAttribute tillämpas på. Den här typen används av objektet XmlSerializer vid serialisering eller deserialisering som innehåller det.

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)

Parametrar

type
Type

För Type ett objekt som härletts från medlemmens typ.

Exempel

I följande exempel serialiseras en klass med namnet Orchestra som innehåller ett enda fält med namnet Instruments, som returnerar en matris med Instrument objekt. En andra klass med namnet Brass ärver från Instrument klassen. Exemplet gäller XmlElementAttribute för fältet Instruments och anger Brass typen så att fältet Instruments kan acceptera Brass objekt. Exemplet anger också namnet på XML-elementet genom att ElementName ange egenskapen .

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

Kommentarer

Använd parametern type för att ange en typ som härleds från en basklass. Anta till exempel att en egenskap med namnet MyAnimal returnerar ett Animal objekt. Du vill förbättra objektet så att du skapar en ny klass med namnet Mammal som ärver från Animal klassen. Om du vill instruera XmlSerializer att acceptera Mammal klassen när den MyAnimal serialiserar egenskapen skickar TypeMammal du klassens till konstruktorn.

Gäller för

XmlElementAttribute(String, Type)

Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs
Källa:
XmlElementAttribute.cs

Initierar en ny instans av XmlElementAttribute och anger namnet på XML-elementet och en härledd typ för medlemmen som XmlElementAttribute används. Den här medlemstypen används när XmlSerializer serialiserar objektet som innehåller det.

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)

Parametrar

elementName
String

XML-elementnamnet för den serialiserade medlemmen.

type
Type

För Type ett objekt som härletts från medlemmens typ.

Exempel

I följande exempel serialiseras en klass med namnet Orchestra som innehåller ett enda fält med namnet Instruments, som returnerar en matris med Instrument objekt. En andra klass med namnet Brass ärver från Instrument klassen. Exemplet gäller XmlElementAttribute för fältet Instruments och anger Brass typen så att fältet Instruments kan acceptera Brass objekt. Exemplet anger också namnet på XML-elementet genom att ElementName ange egenskapen .

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

Kommentarer

Som standard XmlSerializer använder medlemsnamnet som XML-elementnamn när du serialiserar en klassinstans. Ett fält med namnet Vehicle genererar till exempel ett XML-element med namnet Vehicle. Men om du behöver ett annat element, till exempel Cars, skickar du det i parametern elementName .

Använd parametern type för att ange en typ som härleds från en basklass. Anta till exempel att en egenskap med namnet MyAnimal returnerar ett Animal objekt. Du vill förbättra objektet så att du skapar en ny klass med namnet Mammal som ärver från Animal klassen. Om du vill instruera XmlSerializer att acceptera Mammal klassen när den MyAnimal serialiserar egenskapen skickar TypeMammal du klassens till konstruktorn.

Gäller för