0

I have a problem with reading and writing data into file notepad using serialization Basically that's about login and sign up I'll give my code here This is the part for sign up form :

public partial class formSignUp : Form
    {
        List<data> game = new List<data>();
        data dt = new data();

        public formSignUp()
        {
            InitializeComponent();
        }        

        private void write()
        {
            try
            {
                using (Stream stream = File.OpenWrite("game.txt"))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    bin.Serialize(stream, dt);
                }
            }
            catch (IOException)
            {

            }
        }

        private void butRegister_Click(object sender, EventArgs e)
        {
            write();
            dt = new data(tbUName.Text, tbPass1.Text);
            game.Add(dt);
        }
    }

and then this is the class data code :

[Serializable()]
    class data
    {
        private string username;
        private string password;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public data(string username, string password)
        {
            this.username = username;
            this.password = password;
        }

        public data()
        { 

        }
    }

i put the file notepad inside folder "bin" with name "game.txt"

the problem is, everytime i call "write()" the character which is written into file "game.txt" is so strange character. so far i got like :

ÿÿÿÿ          <Login, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null   

Login.data usernamepassword

Anybody can help me? Thanks a lot in advance Regards

zmbq
  • 38,013
  • 14
  • 101
  • 171
Han Sien
  • 1
  • 1

1 Answers1

1

That's because you're using a BinaryFormatter, it outputs binary data. You should use one of the textual formatters. There's SoapFormatter and the DataContractJsonSerialized, although it may not be suitible for you.

zmbq
  • 38,013
  • 14
  • 101
  • 171