XmlSerializer.Deserialize Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Désérialise un document XML.
Surcharges
| Nom | Description |
|---|---|
| Deserialize(Stream) |
Désérialise le document XML contenu par le fichier spécifié Stream. |
| Deserialize(TextReader) |
Désérialise le document XML contenu par le fichier spécifié TextReader. |
| Deserialize(XmlSerializationReader) |
Désérialise le document XML contenu par le fichier spécifié XmlSerializationReader. |
| Deserialize(XmlReader) |
Désérialise le document XML contenu par le fichier spécifié XmlReader. |
| Deserialize(XmlReader, String) |
Désérialise le document XML contenu par le style d’encodage et spécifié XmlReader . |
| Deserialize(XmlReader, XmlDeserializationEvents) |
Désérialise un document XML contenu par le document spécifié XmlReader et autorise la substitution d’événements qui se produisent pendant la désérialisation. |
| Deserialize(XmlReader, String, XmlDeserializationEvents) |
Désérialise l’objet à l’aide des données contenues dans le fichier spécifié XmlReader. |
Deserialize(Stream)
Désérialise le document XML contenu par le fichier spécifié Stream.
public:
System::Object ^ Deserialize(System::IO::Stream ^ stream);
public object Deserialize(System.IO.Stream stream);
member this.Deserialize : System.IO.Stream -> obj
Public Function Deserialize (stream As Stream) As Object
Paramètres
Retours
Désérialisé Object .
Exemples
L’exemple suivant désérialise un objet à l’aide d’un Stream objet.
using System;
using System.IO;
using System.Xml.Serialization;
// This is the class that will be deserialized.
public class OrderedItem
{
[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 Test
{
public static void Main()
{
Test t = new Test();
// Read a purchase order.
t.DeserializeObject("simple.xml");
}
private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with Stream");
// Create an instance of the XmlSerializer.
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
// Declare an object variable of the type to be deserialized.
OrderedItem i;
using (Stream reader = new FileStream(filename, FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
i = (OrderedItem)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 deserialized.
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()
' Read a purchase order.
t.DeserializeObject("simple.xml")
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))
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
Using reader As New Filestream(filename, FileMode.Open)
' Call the Deserialize method to restore the object's state.
i = CType(serializer.Deserialize(reader), OrderedItem)
End Using
' 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
<?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>
Remarques
La désérialisation est le processus de lecture d’un document XML et de construction d’un objet fortement typé au schéma XML (XSD) du document.
Avant la désérialisation, un XmlSerializer doit être construit à l’aide du type de l’objet en cours de désérialisation.
Utilisez le stream paramètre pour spécifier un objet dérivé de la Stream classe, qui est conçu pour écrire dans des flux. Les classes qui dérivent de la Stream classe sont les suivantes :
Note
Impossible XmlSerializer de désérialiser les tableaux suivants : tableaux de ArrayList et tableaux de List<T>.
Voir aussi
- XmlAttributes
- CanDeserialize(XmlReader)
- Serialize(TextWriter, Object)
- Présentation de la sérialisation XML
- Guide pratique pour spécifier un autre nom d’élément pour un flux XML
- Contrôle de la sérialisation XML à l’aide d’attributs
- Exemples de sérialisation XML
- Outil de définition de schéma XML (Xsd.exe)
S’applique à
Deserialize(TextReader)
Désérialise le document XML contenu par le fichier spécifié TextReader.
public:
System::Object ^ Deserialize(System::IO::TextReader ^ textReader);
public object Deserialize(System.IO.TextReader textReader);
member this.Deserialize : System.IO.TextReader -> obj
Public Function Deserialize (textReader As TextReader) As Object
Paramètres
- textReader
- TextReader
Qui TextReader contient le document XML à désérialiser.
Retours
Désérialisé Object .
Exceptions
Une erreur s’est produite lors de la désérialisation. L’exception d’origine est disponible à l’aide de la InnerException propriété.
Exemples
L’exemple suivant désérialise un objet à l’aide d’un TextReader objet.
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
// This is the class that will be deserialized.
public class OrderedItem
{
[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 Test
{
public static void Main()
{
Test t = new Test();
// Read a purchase order.
t.DeserializeObject("simple.xml");
}
private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with TextReader");
// Create an instance of the XmlSerializer specifying type.
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
// Create a TextReader to read the file.
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
TextReader reader = new StreamReader(fs);
// Declare an object variable of the type to be deserialized.
OrderedItem i;
// Use the Deserialize method to restore the object's state.
i = (OrderedItem) 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.Text
Imports System.Xml.Serialization
' This is the class that will be deserialized.
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()
' Read a purchase order.
t.DeserializeObject("simple.xml")
End Sub
Private Sub DeserializeObject(filename As String)
Console.WriteLine("Reading with TextReader")
' Create an instance of the XmlSerializer specifying type.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' Create a TextReader to read the file.
Dim fs as New FileStream(filename, FileMode.OpenOrCreate)
Dim reader As New StreamReader(fs)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Use the Deserialize method to restore the object's state.
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
Remarques
La désérialisation est le processus de lecture d’une instance d’un document XML et de construction d’un objet fortement typé au schéma XML (XSD) du document.
Avant la désérialisation, un XmlSerializer doit être construit à l’aide du type de l’objet en cours de désérialisation.
Classes qui héritent d’include TextReaderStringReader et StreamReader. Si vous utilisez un StreamReader pour désérialiser un objet, vous devez construire l’objet StreamReader avec un élément approprié Encoding. L’encodage spécifié par le document XML est ignoré.
Note
Pour utiliser l’encodage spécifié par le document XML, utilisez plutôt la Deserialize surcharge qui prend un XmlReader . Le XmlReader fichier détecte et utilise automatiquement l’encodage spécifié par le document XML.
Note
Impossible XmlSerializer de désérialiser les tableaux suivants : tableaux de ArrayList et tableaux de List<T>.
Voir aussi
- XmlAttributes
- CanDeserialize(XmlReader)
- Serialize(TextWriter, Object)
- Présentation de la sérialisation XML
- Guide pratique pour spécifier un autre nom d’élément pour un flux XML
- Contrôle de la sérialisation XML à l’aide d’attributs
- Exemples de sérialisation XML
- Outil de définition de schéma XML (Xsd.exe)
S’applique à
Deserialize(XmlSerializationReader)
Désérialise le document XML contenu par le fichier spécifié XmlSerializationReader.
protected:
virtual System::Object ^ Deserialize(System::Xml::Serialization::XmlSerializationReader ^ reader);
protected virtual object Deserialize(System.Xml.Serialization.XmlSerializationReader reader);
abstract member Deserialize : System.Xml.Serialization.XmlSerializationReader -> obj
override this.Deserialize : System.Xml.Serialization.XmlSerializationReader -> obj
Protected Overridable Function Deserialize (reader As XmlSerializationReader) As Object
Paramètres
- reader
- XmlSerializationReader
Qui XmlSerializationReader contient le document XML à désérialiser.
Retours
Objet désérialisé.
Exceptions
Toute tentative d’accès à la méthode lorsque la méthode n’est pas substituée dans une classe descendante.
S’applique à
Deserialize(XmlReader)
Désérialise le document XML contenu par le fichier spécifié XmlReader.
public:
System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader);
public object Deserialize(System.Xml.XmlReader xmlReader);
member this.Deserialize : System.Xml.XmlReader -> obj
Public Function Deserialize (xmlReader As XmlReader) As Object
Paramètres
Retours
Désérialisé Object .
Exceptions
Une erreur s’est produite lors de la désérialisation. L’exception d’origine est disponible à l’aide de la InnerException propriété.
Exemples
L’exemple suivant désérialise un objet à l’aide d’un XmlReader.
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be deserialized.
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 Test();
// Read a purchase order.
t.DeserializeObject("simple.xml");
}
private void DeserializeObject(string filename)
{
Console.WriteLine("Reading with XmlReader");
// Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new
XmlSerializer(typeof(OrderedItem));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
// Declare an object variable of the type to be deserialized.
OrderedItem i;
// Use the Deserialize method to restore the object's state.
i = (OrderedItem)serializer.Deserialize(reader);
fs.Close();
// 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.Text
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be deserialized.
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()
' Read a purchase order.
t.DeserializeObject("simple.xml")
End Sub
Private Sub DeserializeObject(ByVal filename As String)
Console.WriteLine("Reading with XmlReader")
' Create an instance of the XmlSerializer specifying type and namespace.
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' A FileStream is needed to read the XML document.
Dim fs As New FileStream(filename, FileMode.Open)
Dim reader As XmlReader = XmlReader.Create(fs)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Use the Deserialize method to restore the object's state.
i = CType(serializer.Deserialize(reader), OrderedItem)
fs.Close()
' 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
<?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>
Remarques
La désérialisation est le processus de lecture d’une instance d’un document XML et de construction d’un objet fortement typé au schéma XML (XSD) du document.
Avant la désérialisation, un XmlSerializer doit être construit à l’aide du type de l’objet en cours de désérialisation.
Le XmlReader fichier détecte et utilise automatiquement l’encodage spécifié par le document XML.
Note
Impossible XmlSerializer de désérialiser les tableaux suivants : tableaux de ArrayList et tableaux de List<T>.
Voir aussi
- XmlAttributes
- CanDeserialize(XmlReader)
- Serialize(TextWriter, Object)
- Présentation de la sérialisation XML
- Guide pratique pour spécifier un autre nom d’élément pour un flux XML
- Contrôle de la sérialisation XML à l’aide d’attributs
- Exemples de sérialisation XML
- Outil de définition de schéma XML (Xsd.exe)
S’applique à
Deserialize(XmlReader, String)
Désérialise le document XML contenu par le style d’encodage et spécifié XmlReader .
public:
System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::String ^ encodingStyle);
public object Deserialize(System.Xml.XmlReader xmlReader, string encodingStyle);
member this.Deserialize : System.Xml.XmlReader * string -> obj
Public Function Deserialize (xmlReader As XmlReader, encodingStyle As String) As Object
Paramètres
- encodingStyle
- String
Style d’encodage du xml sérialisé.
Retours
Objet désérialisé.
Exceptions
Une erreur s’est produite lors de la désérialisation. L’exception d’origine est disponible à l’aide de la InnerException propriété.
Remarques
La désérialisation est le processus de lecture d’une instance d’un document XML et de construction d’un objet fortement typé au schéma XML (XSD) du document.
Avant la désérialisation, un XmlSerializer doit être construit à l’aide du type de l’objet en cours de désérialisation.
Définissez le paramètre sur «http://schemas.xmlsoap.org/soap/encoding/ » pour l’encodage SOAP version 1.1 ; sinon, définissez-le encodingStyle sur «http://www.w3.org/2001/12/soap-encoding » pour l’encodage SOAP version 1.2.
Note Impossible XmlSerializer de désérialiser les tableaux suivants : tableaux de ArrayList et tableaux de List<T>.
Voir aussi
- XmlAttributes
- CanDeserialize(XmlReader)
- Serialize(TextWriter, Object)
- Présentation de la sérialisation XML
- Guide pratique pour spécifier un autre nom d’élément pour un flux XML
- Contrôle de la sérialisation XML à l’aide d’attributs
- Exemples de sérialisation XML
- Outil de définition de schéma XML (Xsd.exe)
S’applique à
Deserialize(XmlReader, XmlDeserializationEvents)
Désérialise un document XML contenu par le document spécifié XmlReader et autorise la substitution d’événements qui se produisent pendant la désérialisation.
public:
System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::Xml::Serialization::XmlDeserializationEvents events);
public object Deserialize(System.Xml.XmlReader xmlReader, System.Xml.Serialization.XmlDeserializationEvents events);
member this.Deserialize : System.Xml.XmlReader * System.Xml.Serialization.XmlDeserializationEvents -> obj
Public Function Deserialize (xmlReader As XmlReader, events As XmlDeserializationEvents) As Object
Paramètres
- events
- XmlDeserializationEvents
Instance de la classe XmlDeserializationEvents.
Retours
Désérialisé Object .
Remarques
Objet désérialisé.
S’applique à
Deserialize(XmlReader, String, XmlDeserializationEvents)
Désérialise l’objet à l’aide des données contenues dans le fichier spécifié XmlReader.
public:
System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::String ^ encodingStyle, System::Xml::Serialization::XmlDeserializationEvents events);
public object Deserialize(System.Xml.XmlReader xmlReader, string encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events);
member this.Deserialize : System.Xml.XmlReader * string * System.Xml.Serialization.XmlDeserializationEvents -> obj
Public Function Deserialize (xmlReader As XmlReader, encodingStyle As String, events As XmlDeserializationEvents) As Object
Paramètres
- encodingStyle
- String
Encodage utilisé.
- events
- XmlDeserializationEvents
Instance de la classe XmlDeserializationEvents.
Retours
Objet désérialisé.
- Attributs
Remarques
Cette méthode est requise pour la désérialisation des en-têtes inconnus pour les scénarios de service web uniquement. Cette méthode vous permet d’éviter la synchronisation d’événements dans les méthodes de service web.