XmlSerializer.Serialize Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Serialiseert een object in een XML-document.
Overloads
| Name | Description |
|---|---|
| Serialize(XmlWriter, Object) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven XmlWriter. |
| Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven XmlWriterXML-naamruimten en codering. |
| Serialize(XmlWriter, Object, XmlSerializerNamespaces, String) |
Serialiseert het opgegeven object en schrijft het XML-document naar een bestand met behulp van de opgegeven en verwijst naar de opgegeven XmlWriter naamruimten en coderingsstijl. |
| Serialize(XmlWriter, Object, XmlSerializerNamespaces) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven en verwijst naar de opgegeven XmlWriter naamruimten. |
| Serialize(TextWriter, Object, XmlSerializerNamespaces) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven en verwijst naar de opgegeven TextWriter naamruimten. |
| Serialize(Object, XmlSerializationWriter) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven XmlSerializationWriter. |
| Serialize(TextWriter, Object) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven TextWriter. |
| Serialize(Stream, Object) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven Stream. |
| Serialize(Stream, Object, XmlSerializerNamespaces) |
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven Stream die verwijst naar de opgegeven naamruimten. |
Serialize(XmlWriter, Object)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
public:
void Serialize(System::Xml::XmlWriter ^ xmlWriter, System::Object ^ o);
public void Serialize(System.Xml.XmlWriter xmlWriter, object o);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o);
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o);
member this.Serialize : System.Xml.XmlWriter * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj -> unit
Public Sub Serialize (xmlWriter As XmlWriter, o As Object)
Parameters
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Voorbeelden
In het volgende voorbeeld wordt een object geserialiseerd met behulp van een XmlWriter.
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem4
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test4
{
public static void Main()
{
Test4 t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With XmlTextWriter");
XmlSerializer serializer = new(typeof(OrderedItem4));
OrderedItem4 i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlTextWriter writer = new(fs, Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i);
writer.Close();
}
}
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With XmlTextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlTextWriter using a FileStream.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New XmlTextWriter(fs, Encoding.Unicode)
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Opmerkingen
De Serialize methode converteert de openbare velden en lees-/schrijfeigenschappen van een object naar XML. Er worden geen methoden, indexeerfuncties, privévelden of alleen-lezen-eigenschappen geconverteerd.
Geef in de xmlWriter parameter een object op dat is afgeleid van de abstracte XmlWriter klasse. De XmlTextWriter afgeleiden van de XmlWriter.
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Deserialize(Stream)
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
public:
void Serialize(System::Xml::XmlWriter ^ xmlWriter, System::Object ^ o, System::Xml::Serialization::XmlSerializerNamespaces ^ namespaces, System::String ^ encodingStyle, System::String ^ id);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle, string? id);
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle, string? id);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle, string? id);
public void Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string * string -> unit
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string * string -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string * string -> unit
Public Sub Serialize (xmlWriter As XmlWriter, o As Object, namespaces As XmlSerializerNamespaces, encodingStyle As String, id As String)
Parameters
- o
- Object
Het object dat moet worden geserialiseerd.
- namespaces
- XmlSerializerNamespaces
Een exemplaar van de XmlSerializerNamespaces naamruimten en voorvoegsels die moeten worden gebruikt.
- encodingStyle
- String
De codering die in het document wordt gebruikt.
- id
- String
Voor MET SOAP gecodeerde berichten wordt de basis gebruikt voor het genereren van id-kenmerken.
- Kenmerken
Opmerkingen
De id-parameter levert de basistekenreeks die wordt gebruikt om SOAP-id's te maken. Deze zijn standaard 'id1', 'id2' enzovoort. Maar als de parameter is ingesteld op 'myBase', zijn de gegenereerde waarden 'myBaseid1', 'myBaseid2' enzovoort. Deze functionaliteit wordt gebruikt om unieke id-waarden te maken in het hele SOAP-bericht.
Van toepassing op
Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
Serialiseert het opgegeven object en schrijft het XML-document naar een bestand met behulp van de opgegeven en verwijst naar de opgegeven XmlWriter naamruimten en coderingsstijl.
public:
void Serialize(System::Xml::XmlWriter ^ xmlWriter, System::Object ^ o, System::Xml::Serialization::XmlSerializerNamespaces ^ namespaces, System::String ^ encodingStyle);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle);
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle);
public void Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string -> unit
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces * string -> unit
Public Sub Serialize (xmlWriter As XmlWriter, o As Object, namespaces As XmlSerializerNamespaces, encodingStyle As String)
Parameters
- o
- Object
Het object dat moet worden geserialiseerd.
- namespaces
- XmlSerializerNamespaces
Waarnaar XmlSerializerNamespaces wordt verwezen door het object.
- encodingStyle
- String
De coderingsstijl van de geserialiseerde XML.
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Opmerkingen
Wanneer de Serialize methode wordt aangeroepen, worden de openbare velden en lees-/schrijfeigenschappen van een object geconverteerd naar XML. Methoden, indexeerfuncties, privévelden en alleen-lezeneigenschappen worden niet geserialiseerd.
Gebruik de xmlWriter parameter om een object op te geven dat is afgeleid van de abstracte XmlWriter klasse, die is ontworpen voor het schrijven van XML-documenten. De XmlTextWriter afgeleiden van de XmlWriter.
Stel de encodingStyle parameter in op 'http://schemas.xmlsoap.org/soap/encoding/" voor SOAP-versie 1.1-codering; anders stelt u deze in op "http://www.w3.org/2001/12/soap-encoding" voor SOAP-versie 1.2-codering.
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Deserialize(Stream)
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(XmlWriter, Object, XmlSerializerNamespaces)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
public:
void Serialize(System::Xml::XmlWriter ^ xmlWriter, System::Object ^ o, System::Xml::Serialization::XmlSerializerNamespaces ^ namespaces);
public void Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.Xml.XmlWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
Public Sub Serialize (xmlWriter As XmlWriter, o As Object, namespaces As XmlSerializerNamespaces)
Parameters
- namespaces
- XmlSerializerNamespaces
Waarnaar XmlSerializerNamespaces wordt verwezen door het object.
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Voorbeelden
In het volgende voorbeeld wordt een object geserialiseerd met een XmlWriter. In het voorbeeld worden XmlSerializerNamespaces ook twee naamruimten aan het object gemaakt en toegevoegd. Verschillende exemplaren van de XmlElementAttribute klasse worden toegepast op de klasseleden om de naamruimte voor elk element op te geven.
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem5
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test5
{
public static void Main()
{
Test5 t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With XmlTextWriter");
XmlSerializer serializer = new(typeof(OrderedItem5));
OrderedItem5 i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns = new();
// Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlTextWriter writer = new(fs, new UTF8Encoding());
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i, ns);
writer.Close();
}
}
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With XmlTextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create an XmlTextWriter using a FileStream.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New XmlTextWriter(fs, New UTF8Encoding())
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
End Class
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Opmerkingen
Wanneer de Serialize methode wordt aangeroepen, worden de openbare velden en lees-/schrijfeigenschappen van een object geconverteerd naar XML. Methoden, indexeerfuncties, privévelden en alleen-lezeneigenschappen worden niet geserialiseerd.
Gebruik de xmlWriter parameter om een object op te geven dat is afgeleid van de abstracte XmlWriter klasse, die is ontworpen voor het schrijven van XML-documenten. De XmlTextWriter afgeleiden van de XmlWriter.
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Deserialize(Stream)
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(TextWriter, Object, XmlSerializerNamespaces)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven en verwijst naar de opgegeven TextWriter naamruimten.
public:
void Serialize(System::IO::TextWriter ^ textWriter, System::Object ^ o, System::Xml::Serialization::XmlSerializerNamespaces ^ namespaces);
public void Serialize(System.IO.TextWriter textWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.TextWriter textWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
public void Serialize(System.IO.TextWriter textWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.TextWriter textWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
member this.Serialize : System.IO.TextWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.TextWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.TextWriter * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
Public Sub Serialize (textWriter As TextWriter, o As Object, namespaces As XmlSerializerNamespaces)
Parameters
- textWriter
- TextWriter
De TextWriter functie voor het schrijven van het XML-document.
- namespaces
- XmlSerializerNamespaces
De XmlSerializerNamespaces naamruimten voor het gegenereerde XML-document.
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Voorbeelden
In het volgende voorbeeld wordt een object geserialiseerd met een TextWriter. In het voorbeeld wordt ook een XmlSerializerNamespaces object gemaakt en worden er twee naamruimten aan het object toegevoegd. De klasse die het geserialiseerde object definieert, wordt ook toegeschreven aan XmlElementAttribute kenmerken om de naamruimte voor elk element op te geven.
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem1
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test1
{
public static void Main(string[] args)
{
Test1 t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With TextWriter");
// Create an XmlSerializer instance using the type.
XmlSerializer serializer = new(typeof(OrderedItem1));
OrderedItem1 i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns = new();
// Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a StreamWriter to write with.
StreamWriter writer = new(filename);
/* Serialize using the object using the TextWriter
and namespaces. */
serializer.Serialize(writer, i, ns);
writer.Close();
}
}
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With TextWriter")
' Create an XmlSerializer instance using the type.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
i.ItemName = "Widget"
i.Description = "Regular Widget"
i.Quantity = 10
i.UnitPrice = CDec(2.3)
i.Calculate()
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two namespaces with prefixes.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create a StreamWriter to write with.
Dim writer As New StreamWriter(filename)
' Serialize using the object using the TextWriter
' and namespaces.
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
End Class
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Opmerkingen
Wanneer de Serialize methode wordt aangeroepen, worden de openbare velden en lees-/schrijfeigenschappen van een object geconverteerd naar XML. Methoden, indexeerfuncties, privévelden en alleen-lezeneigenschappen worden niet geserialiseerd.
Gebruik de textWriter parameter om een object op te geven dat is afgeleid van de abstracte TextWriter klasse. Klassen die zijn afgeleid van TextWriter klasse zijn onder andere:
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(Object, XmlSerializationWriter)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven XmlSerializationWriter.
protected:
virtual void Serialize(System::Object ^ o, System::Xml::Serialization::XmlSerializationWriter ^ writer);
protected virtual void Serialize(object? o, System.Xml.Serialization.XmlSerializationWriter writer);
protected virtual void Serialize(object o, System.Xml.Serialization.XmlSerializationWriter writer);
abstract member Serialize : obj * System.Xml.Serialization.XmlSerializationWriter -> unit
override this.Serialize : obj * System.Xml.Serialization.XmlSerializationWriter -> unit
Protected Overridable Sub Serialize (o As Object, writer As XmlSerializationWriter)
Parameters
- writer
- XmlSerializationWriter
De XmlSerializationWriter functie voor het schrijven van het XML-document.
Uitzonderingen
Er wordt geprobeerd toegang te krijgen tot de methode wanneer de methode niet wordt overschreven in een onderliggende klasse.
Van toepassing op
Serialize(TextWriter, Object)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
Serialiseert de opgegeven Object en schrijft het XML-document naar een bestand met behulp van de opgegeven TextWriter.
public:
void Serialize(System::IO::TextWriter ^ textWriter, System::Object ^ o);
public void Serialize(System.IO.TextWriter textWriter, object o);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.TextWriter textWriter, object? o);
public void Serialize(System.IO.TextWriter textWriter, object? o);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.TextWriter textWriter, object? o);
member this.Serialize : System.IO.TextWriter * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.TextWriter * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.TextWriter * obj -> unit
Public Sub Serialize (textWriter As TextWriter, o As Object)
Parameters
- textWriter
- TextWriter
De TextWriter functie voor het schrijven van het XML-document.
- Kenmerken
Voorbeelden
In het volgende voorbeeld wordt een object geserialiseerd met behulp van een TextWriter.
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test
{
public static void Main(string[] args)
{
Test t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With TextWriter");
XmlSerializer serializer = new(typeof(OrderedItem));
OrderedItem i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
/* Create a StreamWriter to write with. First create a FileStream
object, and create the StreamWriter specifying an Encoding to use. */
FileStream fs = new(filename, FileMode.Create);
StreamWriter writer = new(fs, new UTF8Encoding());
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, i);
writer.Close();
}
}
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
'A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With TextWriter")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create a StreamWriter to write with. First create a FileStream
' object, and create the StreamWriter specifying an Encoding to use.
Dim fs As New FileStream(filename, FileMode.Create)
Dim writer As New StreamWriter(fs, New UTF8Encoding())
' Serialize using the XmlTextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Opmerkingen
De Serialize methode converteert de openbare velden en lees-/schrijfeigenschappen van een object naar XML. Er worden geen methoden, indexeerfuncties, privévelden of alleen-lezen-eigenschappen geconverteerd.
Geef in de textWriter parameter een object op dat is afgeleid van de abstracte TextWriter klasse. Klassen die zijn afgeleid van TextWriter :
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(Stream, Object)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
public:
void Serialize(System::IO::Stream ^ stream, System::Object ^ o);
public void Serialize(System.IO.Stream stream, object o);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.Stream stream, object? o);
public void Serialize(System.IO.Stream stream, object? o);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.Stream stream, object? o);
member this.Serialize : System.IO.Stream * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.Stream * obj -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.Stream * obj -> unit
Public Sub Serialize (stream As Stream, o As Object)
Parameters
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Voorbeelden
In het volgende voorbeeld wordt een object geserialiseerd met behulp van een Stream object.
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem2
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test2
{
public static void Main(string[] args)
{
Test2 t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With Stream");
XmlSerializer serializer = new(typeof(OrderedItem2));
OrderedItem2 i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
// Create a FileStream to write with.
FileStream writer = new(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i);
writer.Close();
}
}
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With Stream")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create a FileStream to write with.
Dim writer As New FileStream(filename, FileMode.Create)
' Serialize the object, and close the TextWriter
serializer.Serialize(writer, i)
writer.Close()
End Sub
End Class
<?xml version="1.0"?>
<OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
<inventory:ItemName>Widget</inventory:ItemName>
<inventory:Description>Regular Widget</inventory:Description>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
Opmerkingen
De Serialize methode converteert de openbare velden en lees-/schrijfeigenschappen van een object naar XML. Er worden geen methoden, indexeerfuncties, privévelden of alleen-lezen-eigenschappen geconverteerd.
Geef in de stream parameter een object op dat is afgeleid van de abstracte Stream klasse. Klassen die zijn afgeleid van Stream :
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Deserialize(Stream)
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)
Van toepassing op
Serialize(Stream, Object, XmlSerializerNamespaces)
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
- Bron:
- XmlSerializer.cs
public:
void Serialize(System::IO::Stream ^ stream, System::Object ^ o, System::Xml::Serialization::XmlSerializerNamespaces ^ namespaces);
public void Serialize(System.IO.Stream stream, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.Stream stream, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
public void Serialize(System.IO.Stream stream, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")]
public void Serialize(System.IO.Stream stream, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);
member this.Serialize : System.IO.Stream * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("XML serializer relies on dynamic code generation which is not available with Ahead of Time compilation")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.Stream * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")>]
member this.Serialize : System.IO.Stream * obj * System.Xml.Serialization.XmlSerializerNamespaces -> unit
Public Sub Serialize (stream As Stream, o As Object, namespaces As XmlSerializerNamespaces)
Parameters
- namespaces
- XmlSerializerNamespaces
Waarnaar XmlSerializerNamespaces wordt verwezen door het object.
- Kenmerken
Uitzonderingen
Er is een fout opgetreden tijdens serialisatie. De oorspronkelijke uitzondering is beschikbaar met behulp van de InnerException eigenschap.
Voorbeelden
In het volgende voorbeeld wordt een object met een Stream object geserialiseerd. In het voorbeeld worden XmlSerializerNamespaces ook twee naamruimten aan het object gemaakt en toegevoegd. De klasse die het geserialiseerde object definieert, wordt ook toegeschreven aan XmlElementAttribute kenmerken om de naamruimte voor elk element op te geven.
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class OrderedItem3
{
[XmlElement(Namespace = "http://www.cpandl.com")]
public string ItemName;
[XmlElement(Namespace = "http://www.cpandl.com")]
public string Description;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal UnitPrice;
[XmlElement(Namespace = "http://www.cpandl.com")]
public int Quantity;
[XmlElement(Namespace = "http://www.cohowinery.com")]
public decimal LineTotal;
// A custom method used to calculate price per item.
public void Calculate()
{
LineTotal = UnitPrice * Quantity;
}
}
public class Test3
{
public static void Main()
{
Test3 t = new();
// Write a purchase order.
t.SerializeObject("simple.xml");
t.DeserializeObject("simple.xml");
}
private void SerializeObject(string filename)
{
Console.WriteLine("Writing With Stream");
XmlSerializer serializer =
new(typeof(OrderedItem3));
OrderedItem3 i = new()
{
ItemName = "Widget",
Description = "Regular Widget",
Quantity = 10,
UnitPrice = (decimal)2.30
};
i.Calculate();
// Create an XmlSerializerNamespaces object.
XmlSerializerNamespaces ns = new();
// Add two prefix-namespace pairs.
ns.Add("inventory", "http://www.cpandl.com");
ns.Add("money", "http://www.cohowinery.com");
// Create a FileStream to write with.
FileStream writer = new(filename, FileMode.Create);
// Serialize the object, and close the TextWriter
serializer.Serialize(writer, i, ns);
writer.Close();
}
private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
XmlSerializer serializer = new(typeof(OrderedItem3));
// Writing the file requires a Stream.
Stream reader = new FileStream(filename, FileMode.Open);
// Declare an object variable of the type to be deserialized.
OrderedItem3 i;
/* Use the Deserialize method to restore the object's state
using data from the XML document. */
i = (OrderedItem3)serializer.Deserialize(reader);
// Write out the properties of the object.
Console.Write(
i.ItemName + "\t" +
i.Description + "\t" +
i.UnitPrice + "\t" +
i.Quantity + "\t" +
i.LineTotal);
}
}
Imports System.IO
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class OrderedItem
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public ItemName As String
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Description As String
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public UnitPrice As Decimal
<XmlElement(Namespace := "http://www.cpandl.com")> _
Public Quantity As Integer
<XmlElement(Namespace := "http://www.cohowinery.com")> _
Public LineTotal As Decimal
' A custom method used to calculate price per item.
Public Sub Calculate()
LineTotal = UnitPrice * Quantity
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
' Write a purchase order.
t.SerializeObject("simple.xml")
t.DeserializeObject("simple.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Writing With Stream")
Dim serializer As New XmlSerializer(GetType(OrderedItem))
Dim i As New OrderedItem()
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
.Calculate()
End With
' Create an XmlSerializerNamespaces object.
Dim ns As New XmlSerializerNamespaces()
' Add two prefix-namespace pairs.
ns.Add("inventory", "http://www.cpandl.com")
ns.Add("money", "http://www.cohowinery.com")
' Create a FileStream to write with.
Dim writer As New FileStream(filename, FileMode.Create)
' Serialize the object, and close the TextWriter
serializer.Serialize(writer, i, ns)
writer.Close()
End Sub
Private Sub DeserializeObject(ByVal filename As String)
Console.WriteLine("Reading with Stream")
' Create an instance of the XmlSerializer.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' Writing the file requires a Stream.
Dim reader As New FileStream(filename, FileMode.Open)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Use the Deserialize method to restore the object's state
' using data from the XML document.
i = CType(serializer.Deserialize(reader), OrderedItem)
' Write out the properties of the object.
Console.Write(i.ItemName & ControlChars.Tab & _
i.Description & ControlChars.Tab & _
i.UnitPrice & ControlChars.Tab & _
i.Quantity & ControlChars.Tab & _
i.LineTotal)
End Sub
End Class
Opmerkingen
Wanneer de Serialize methode wordt aangeroepen, worden de openbare velden en lees-/schrijfeigenschappen van een object geconverteerd naar XML. Methoden, indexeerfuncties, privévelden en alleen-lezeneigenschappen worden niet geserialiseerd.
Gebruik de stream parameter om een object op te geven dat is afgeleid van de abstracte Stream klasse, die is ontworpen om naar streams te schrijven. Klassen die zijn afgeleid van de Stream klasse zijn onder andere:
Note
Het XmlSerializer kan het volgende niet serialiseren: matrices van ArrayList en matrices van List<T>.
Zie ook
- XmlAttributes
- Deserialize(Stream)
- Inleiding tot XML-serialisatie
- Procedure: Geef een alternatieve elementnaam op voor een XML-stream
- XML-serialisatie beheren met behulp van kenmerken
- Voorbeelden van XML-serialisatie
- Xml Schema Definition Tool (Xsd.exe)