Serialization error

Papirata

New member
Joined
Apr 13, 2020
Messages
1
Programming Experience
Beginner
i am a beginner and i am learning how to use serialization y have this code and I am ave to serialize, but when I am trying to deserialize the system is giving me an error, hope somebody can help me.


this is the link of my code on GITHUB.
 
We don't really want a link to your GitHub account. We want you to post the relevant code and ONLY the relevant code directly in this thread, so that we can see it without going anywhere or doing anything. We can then copy it as required. The code should be usable on pretty much on its own. If yours isn't, create a test project that isolates the problem functionality from the rest of your application. You almost certainly should have done that already anyway, as part of your own investigation. Also, telling us that an error occurs without specifying where it occurs and what the error message is is like going to the doctor and telling them you're sick but refusing to describe your symptoms. ALWAYS provide a FULL and CLEAR explanation of the problem.
 
As a favor to our OP, here is his code:
Program.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace practicando_herencias
{
    class Program
    {
        public static List<Gato> personas = new List<Gato>();
        
        static void Main(string[] args)
        {

            listado_personas();

        }

        public static void agregar_persona()
        {
            Console.Clear();
            Console.WriteLine("ingrese un nombre");
            string nombre = Console.ReadLine();
            Console.WriteLine("Ingrese un apellido");
            string apellido = Console.ReadLine();
            Console.WriteLine("igrese la edad");
            int edad = Convert.ToInt32(Console.ReadLine());

            Gato insertar = new Gato();
            insertar.nombre = nombre;
            insertar.apellido = apellido;
            insertar.edad = edad;
            personas.Add(insertar);


            IFormatter formatter = new BinaryFormatter();
           Stream stream = new FileStream("C:\\Users\\enman\\OneDrive\\Escritorio\\C#\\practicando_herencias\\serializacion.txt", FileMode.Create, FileAccess.Write);

            formatter.Serialize(stream, personas);
            stream.Close();

            Console.WriteLine("finalizado");
            Console.ReadKey();
            
        }

        public static void listado_personas()
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("C:\\Users\\enman\\OneDrive\\Escritorio\\C#\\practicando_herencias\\serializacion.txt", FileMode.Open, FileAccess.Read);
            
            Gato poner =  (Gato)formatter.Deserialize(stream);

            personas.Add(poner);
            Console.WriteLine(personas);

            Console.ReadKey();


        }
    }
}

Assuming that he used agregar_persona() to serialize his data, then listado_personas() will fail to deserialize because the former saved a List<Gato>, but he is trying to retrieve it and cast that into a Gato.

As a quick aside, it's a really bad idea to do binary serialization into a file and give it a filename that suggests it is a plain text file (e.g. .TXT). Although a filename is just a name, by convention the filename's extension suggests what the content encoding/formatting is.
 
a filename that suggests it is a plain text file (e.g. .TXT). Although a filename is just a name, by convention the filename's extension suggests what the content encoding/formatting is.
I concur, while it is a minor thing, but yet still relevant to the type of data stored within the file. Why not write a .bin file instead? I'm glad to see the OP wasn't silly enough to try do this in Json. As Json isn't very good at handling raw bytes. A common mistake I see a lot of new comers making. Also aside from Skydivers point, be careful how you interpret user input in line 28. You should try parsing the user input instead of straight out trying to convert it. Also check your scope of "Gato" on line 53. Where is it defined?
 

Latest posts

Back
Top Bottom