Deserialización de un objeto con XmlSerializerHow to deserialize an object using XmlSerializer

Al deserializar un objeto, el formato de transporte determina si creará una secuencia u objeto de archivo.When you deserialize an object, the transport format determines whether you will create a stream or file object. Una vez determinado el formato de transporte, puede llamar Serialize o los métodos Deserialize, como se requiera.After the transport format is determined, you can call the Serialize or Deserialize methods, as required.

Para deserializar un objetoTo deserialize an object

  1. Construya unXmlSerializer utilizando el tipo del objeto para deserializar.Construct a XmlSerializer using the type of the object to deserialize.

  2. Llame al método Deserialize para generar una réplica del objeto.Call the Deserialize method to produce a replica of the object. Al deserializar, debe convertir el objeto devuelto al tipo del original, tal y como se muestra en el ejemplo siguiente, que deserializa el objeto en un archivo (aunque también se podía deserializar en una secuencia).When deserializing, you must cast the returned object to the type of the original, as shown in the following example, which deserializes the object from a file (although it could also be deserialized from a stream).

    ' Construct an instance of the XmlSerializer with the type
    ' of object that is being deserialized.
    Dim mySerializer As New XmlSerializer(GetType(MySerializableClass))
    ' To read the file, create a FileStream.
    Dim myFileStream As New FileStream("myFileName.xml", FileMode.Open)
    ' Call the Deserialize method and cast to the object type.
    Dim myObject = CType( _
    mySerializer.Deserialize(myFileStream), MySerializableClass)
    
    // Construct an instance of the XmlSerializer with the type
    // of object that is being deserialized.
    var mySerializer = new XmlSerializer(typeof(MySerializableClass));
    // To read the file, create a FileStream.
    var myFileStream = new FileStream("myFileName.xml", FileMode.Open);
    // Call the Deserialize method and cast to the object type.
    var myObject = (MySerializableClass) mySerializer.Deserialize(myFileStream)
    

Vea tambiénSee also