XmlSerializer-Konstruktor (Type, String)
Initialisiert eine neue Instanz der XmlSerializer-Klasse, die Objekte des angegebenen Typs in XML-Dokumente serialisieren und XML-Dokumente in Objekte des angegebenen Typs deserialisieren kann. Gibt den Standardnamespace für alle XML-Elemente an.
Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Sub New ( _
type As Type, _
defaultNamespace As String _
)
'Usage
Dim type As Type
Dim defaultNamespace As String
Dim instance As New XmlSerializer(type, defaultNamespace)
public XmlSerializer (
Type type,
string defaultNamespace
)
public:
XmlSerializer (
Type^ type,
String^ defaultNamespace
)
public XmlSerializer (
Type type,
String defaultNamespace
)
public function XmlSerializer (
type : Type,
defaultNamespace : String
)
Parameter
- type
Der Objekttyp, den dieser XmlSerializer serialisieren kann.
- defaultNamespace
Der für alle XML-Elemente zu verwendende Standardnamespace.
Beispiel
Im folgenden Beispiel wird ein XmlSerializer erstellt, der ein einfaches Objekt mit dem Namen Widget serialisiert. Im Beispiel werden vor dem Aufruf der Serialize-Methode verschiedene Eigenschaften des Objekts festgelegt.
Private Sub SerializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem), _
"http://www.cpandl.com")
' Create an instance of the class to be serialized.
Dim i As New OrderedItem()
' Insert code to set property values.
' Writing the document requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the TextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
Private Sub DeserializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem), _
"http://www.cpandl.com")
' A FileStream is needed to read the XML document.
Dim fs As New FileStream(filename, FileMode.Open)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Deserialize the object.
i = CType(serializer.Deserialize(fs), OrderedItem)
' Insert code to use the properties and methods of the object.
End Sub
private void SerializeObject(string filename) {
XmlSerializer serializer = new XmlSerializer
(typeof(OrderedItem), "http://www.cpandl.com");
// Create an instance of the class to be serialized.
OrderedItem i = new OrderedItem();
// Insert code to set property values.
// Writing the document requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
}
private void DeserializeObject(string filename) {
XmlSerializer serializer = new XmlSerializer
(typeof(OrderedItem), "http://www.cpandl.com");
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
OrderedItem i;
// Deserialize the object.
i = (OrderedItem) serializer.Deserialize(fs);
// Insert code to use the properties and methods of the object.
}
private:
void SerializeObject( String^ filename )
{
XmlSerializer^ serializer = gcnew XmlSerializer(
OrderedItem::typeid,"http://www.cpandl.com" );
// Create an instance of the class to be serialized.
OrderedItem^ i = gcnew OrderedItem;
// Insert code to set property values.
// Writing the document requires a TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
// Serialize the object, and close the TextWriter
serializer->Serialize( writer, i );
writer->Close();
}
void DeserializeObject( String^ filename )
{
XmlSerializer^ serializer = gcnew XmlSerializer(
OrderedItem::typeid,"http://www.cpandl.com" );
// A FileStream is needed to read the XML document.
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
// Declare an object variable of the type to be deserialized.
OrderedItem^ i;
// Deserialize the object.
i = dynamic_cast<OrderedItem^>(serializer->Deserialize( fs ));
// Insert code to use the properties and methods of the object.
}
private void SerializeObject(String filename)
{
XmlSerializer serializer =
new XmlSerializer(OrderedItem.class.ToType(),
"http://www.cpandl.com");
// Create an instance of the class to be serialized.
OrderedItem i = new OrderedItem();
// Insert code to set property values.
// Writing the document requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
} //SerializeObject
private void DeserializeObject(String filename)
{
XmlSerializer serializer =
new XmlSerializer(OrderedItem.class.ToType(),
"http://www.cpandl.com");
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
OrderedItem i;
// Deserialize the object.
i = (OrderedItem)serializer.Deserialize(fs);
// Insert code to use the properties and methods of the object.
} //DeserializeObject
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
Siehe auch
Referenz
XmlSerializer-Klasse
XmlSerializer-Member
System.Xml.Serialization-Namespace
XmlAttributes-Klasse
Weitere Ressourcen
Einführung in die XML-Serialisierung
Gewusst wie: Angeben eines alternativen Elementnamens für einen XML-Stream
Steuern der XML-Serialisierung mit Attributen
Beispiele für die XML-Serialisierung
XML Schema Definition-Tool (Xsd.exe)