XmlElementAttribute-Konstruktor (Type)
Initialisiert eine neues Instanz der XmlElementAttribute-Klasse und gibt einen Typ für den Member an, auf den das XmlElementAttribute angewendet wird. Dieser Typ wird vom XmlSerializer verwendet, wenn das Objekt serialisiert oder deserialisiert wird, in dem es enthalten ist.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Sub New ( _
type As Type _
)
'Usage
Dim type As Type
Dim instance As New XmlElementAttribute(type)
public XmlElementAttribute (
Type type
)
public:
XmlElementAttribute (
Type^ type
)
public XmlElementAttribute (
Type type
)
public function XmlElementAttribute (
type : Type
)
Parameter
- type
Der Type eines Objekts, das vom Typ des Members abgeleitet ist.
Hinweise
Geben Sie mit dem type-Parameter einen Typ an, der von einer Basisklasse abgeleitet ist. Angenommen, die Eigenschaft MyAnimal gibt ein Animal-Objekt zurück. Das Objekt soll erweitert werden, deshalb erstellen Sie eine neue Klasse mit dem Namen Mammal, die von der Animal-Klasse erbt. Um XmlSerializer anzuweisen, die Klasse Mammal beim Serialisieren der MyAnimal-Eigenschaft zu akzeptieren, übergeben Sie den Type der Mammal-Klasse an den Konstruktor.
Beispiel
Im folgenden Beispiel wird die Orchestra-Klasse serialisiert, die als einziges Feld Instruments enthält, das ein Array von Instrument-Objekten zurückgibt. Eine zweite Klasse Brass erbt von der Instrument-Klasse. Im Beispiel wird XmlElementAttribute auf das Instruments-Feld angewendet und der Brass-Typ angegeben, wodurch das Instruments-Feld Brass-Objekte akzeptieren kann. Im Beispiel wird außerdem der Name des XML-Elements angegeben, indem die ElementName-Eigenschaft festgelegt wird.
Option Strict
Option Explicit
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
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 'Main
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
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);
}
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Instrument
{
public:
String^ Name;
};
public ref class Brass: public Instrument
{
public:
bool IsValved;
};
public ref class Orchestra
{
public:
array<Instrument^>^Instruments;
};
void SerializeObject( String^ filename )
{
// To write the file, a TextWriter is required.
TextWriter^ writer = gcnew StreamWriter( filename );
XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
XmlAttributes^ attrs = gcnew XmlAttributes;
// Creates an XmlElementAttribute that overrides the Instrument type.
XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
attr->ElementName = "Brass";
// Adds the element to the collection of elements.
attrs->XmlElements->Add( attr );
attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
// Creates the object to serialize.
Orchestra^ band = gcnew Orchestra;
// Creates an object of the derived type.
Brass^ i = gcnew Brass;
i->Name = "Trumpet";
i->IsValved = true;
array<Instrument^>^myInstruments = {i};
band->Instruments = myInstruments;
s->Serialize( writer, band );
writer->Close();
}
void DeserializeObject( String^ filename )
{
XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides;
XmlAttributes^ attrs = gcnew XmlAttributes;
// Creates an XmlElementAttribute that override the Instrument type.
XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid );
attr->ElementName = "Brass";
// Adds the element to the collection of elements.
attrs->XmlElements->Add( attr );
attrOverrides->Add( Orchestra::typeid, "Instruments", attrs );
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides );
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Orchestra^ band = dynamic_cast<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;
System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator();
while ( myEnum->MoveNext() )
{
Instrument^ i = safe_cast<Instrument^>(myEnum->Current);
b = dynamic_cast<Brass^>(i);
Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved );
}
}
int main()
{
SerializeObject( "Override.xml" );
DeserializeObject( "Override.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
public class Orchestra
{
public Instrument instruments[];
} //Orchestra
public class Instrument
{
public String name;
} //Instrument
public class Brass extends Instrument
{
public boolean isValved;
} //Brass
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeObject("Override.xml");
test.DeserializeObject("Override.xml");
} //main
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(Brass.class.ToType());
attr.set_ElementName("Brass");
// Adds the element to the collection of elements.
attrs.get_XmlElements().Add(attr);
attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs);
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(Orchestra.class.ToType(), 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();
} //SerializeObject
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(Brass.class.ToType());
attr.set_ElementName("Brass");
// Adds the element to the collection of elements.
attrs.get_XmlElements().Add(attr);
attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs);
// Creates the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(Orchestra.class.ToType(), 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;
for (int iCtr = 0; iCtr < band.instruments.length; iCtr++) {
Instrument i = band.instruments[iCtr];
b = (Brass)i;
Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved));
}
} //DeserializeObject
} //Run
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
XmlElementAttribute-Klasse
XmlElementAttribute-Member
System.Xml.Serialization-Namespace