XmlElementAttribute Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe XmlElementAttribute.
Overload
| Nome | Descrizione |
|---|---|
| XmlElementAttribute() |
Inizializza una nuova istanza della classe XmlElementAttribute. |
| XmlElementAttribute(String) |
Inizializza una nuova istanza della XmlElementAttribute classe e specifica il nome dell'elemento XML. |
| XmlElementAttribute(Type) |
Inizializza una nuova istanza della XmlElementAttribute classe e specifica un tipo per il membro a cui viene applicato l'oggetto XmlElementAttribute . Questo tipo viene utilizzato dall'oggetto durante la serializzazione o la deserializzazione dell'oggetto XmlSerializer che lo contiene. |
| XmlElementAttribute(String, Type) |
Inizializza una nuova istanza di XmlElementAttribute e specifica il nome dell'elemento XML e un tipo derivato per il membro a cui viene applicato l'oggetto XmlElementAttribute . Questo tipo di membro viene utilizzato quando l'oggetto XmlSerializer serializza l'oggetto che lo contiene. |
XmlElementAttribute()
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
Inizializza una nuova istanza della classe XmlElementAttribute.
public:
XmlElementAttribute();
public XmlElementAttribute();
Public Sub New ()
Esempio
Nell'esempio seguente viene applicato a XmlElementAttribute una classe .
public class MyClass
{
[XmlElement()]
public string TeacherName;
}
Public Class MyClass1
<XmlElement()> Public TeacherName As String
End Class
Si applica a
XmlElementAttribute(String)
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
Inizializza una nuova istanza della XmlElementAttribute classe e specifica il nome dell'elemento 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)
Parametri
- elementName
- String
Nome dell'elemento XML del membro serializzato.
Esempio
Nell'esempio seguente viene illustrata una classe semplice che contiene un singolo campo denominato Vehicles. L'esempio applica l'oggetto XmlElementAttribute al campo e include il elementName parametro , in modo da indicare all'oggetto XmlSerializer di generare elementi XML denominati "Cars" anziché "Vehicles".
public class Transportation
{
[XmlElement("Cars")]
public string Vehicles;
}
Public Class Transportation
<XmlElement("Cars")> Public Vehicles As String
End Class
Commenti
Per impostazione predefinita, XmlSerializer usa il nome del membro come nome dell'elemento XML durante la serializzazione di un'istanza di classe. Ad esempio, un campo denominato Vehicle genera un elemento XML denominato Vehicle. Tuttavia, se è necessario un elemento diverso, ad esempio Cars, passarlo nel elementName parametro .
Si applica a
XmlElementAttribute(Type)
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
Inizializza una nuova istanza della XmlElementAttribute classe e specifica un tipo per il membro a cui viene applicato l'oggetto XmlElementAttribute . Questo tipo viene utilizzato dall'oggetto durante la serializzazione o la deserializzazione dell'oggetto XmlSerializer che lo contiene.
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)
Parametri
Esempio
Nell'esempio seguente viene serializzata una classe denominata Orchestra che contiene un singolo campo denominato Instruments, che restituisce una matrice di Instrument oggetti. Una seconda classe denominata Brass eredita dalla Instrument classe . Nell'esempio viene applicato al XmlElementAttributeInstruments campo e viene specificato il Brass tipo, consentendo al Instruments campo di accettare Brass oggetti. Nell'esempio viene inoltre specificato il nome dell'elemento XML impostando la ElementName proprietà .
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
Commenti
Usare il type parametro per specificare un tipo derivato da una classe di base. Si supponga, ad esempio, che una proprietà denominata MyAnimal restituisca un Animal oggetto . Si vuole migliorare l'oggetto, in modo da creare una nuova classe denominata Mammal che eredita dalla Animal classe . Per indicare all'oggetto XmlSerializer di accettare la Mammal classe quando serializza la MyAnimal proprietà , passare l'oggetto Type della Mammal classe al costruttore .
Si applica a
XmlElementAttribute(String, Type)
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
- Origine:
- XmlElementAttribute.cs
Inizializza una nuova istanza di XmlElementAttribute e specifica il nome dell'elemento XML e un tipo derivato per il membro a cui viene applicato l'oggetto XmlElementAttribute . Questo tipo di membro viene utilizzato quando l'oggetto XmlSerializer serializza l'oggetto che lo contiene.
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)
Parametri
- elementName
- String
Nome dell'elemento XML del membro serializzato.
Esempio
Nell'esempio seguente viene serializzata una classe denominata Orchestra che contiene un singolo campo denominato Instruments, che restituisce una matrice di Instrument oggetti. Una seconda classe denominata Brass eredita dalla Instrument classe . Nell'esempio viene applicato al XmlElementAttributeInstruments campo e viene specificato il Brass tipo, consentendo al Instruments campo di accettare Brass oggetti. Nell'esempio viene inoltre specificato il nome dell'elemento XML impostando la ElementName proprietà .
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
Commenti
Per impostazione predefinita, XmlSerializer usa il nome del membro come nome dell'elemento XML durante la serializzazione di un'istanza di classe. Ad esempio, un campo denominato Vehicle genera un elemento XML denominato Vehicle. Tuttavia, se è necessario un elemento diverso, ad esempio Cars, passarlo nel elementName parametro .
Usare il type parametro per specificare un tipo derivato da una classe di base. Si supponga, ad esempio, che una proprietà denominata MyAnimal restituisca un Animal oggetto . Si vuole migliorare l'oggetto, in modo da creare una nuova classe denominata Mammal che eredita dalla Animal classe . Per indicare all'oggetto XmlSerializer di accettare la Mammal classe quando serializza la MyAnimal proprietà , passare l'oggetto Type della Mammal classe al costruttore .