0

New to coding!

How can I get my register form to add the user's details instead of rewriting them everytime? And how can I get my log in form to loop through the XML file to find a matching username and password?

The code that I have does 2 things:

  1. It rewrites the XML file instead of updating it.
  2. It duplicates data.

Here is my User class:

public class User
    {
        public string fname;
        public string lname;
        public string username;
        public string password;

        public string Fname
        {
            get { return fname; }
            set { fname = value; }
        }

        public string Lname
        {
            get { return lname; }
            set {lname = value; }
        }

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

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

        public User() { }

        public User (string fname, string lname, string username, string password)
        {
            this.fname = fname;
            this.lname = lname;
            this.username = username;
            this.password = password;
        }

    }

Here is the sign up form code:

public partial class sign_up_form : Form
    {

        public sign_up_form()
        {
            InitializeComponent();
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            User users = new User();
            users.fname = txt_fname.Text;
            users.lname = txt_lname.Text;
            users.username = txt_username.Text;
            users.password = txt_password.Text;

            XmlSerializer xs = new XmlSerializer(typeof(User));
            using(FileStream fs = new FileStream("Data.xml", FileMode.Create))
            {
                xs.Serialize(fs, users);
            }

        }
    }

This is the XML file:

<?xml version="1.0"?>

-<User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<fname>asdf</fname>

<lname>asdf</lname>

<username>asdf</username>

<password>asdf</password>

<Fname>asdf</Fname>

<Lname>asdf</Lname>

<Username>asdf</Username>

<Password>asdf</Password>

</User>

I do not have any code for the log in form but it only has 2 text boxes (user and pass) and a log in button.

Any advice is appreciated.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • How do you suggest I update the file rather than creating a new one – unicornswutt Apr 24 '19 at 20:11
  • [*Please*](https://stackoverflow.com/questions/2558787/how-to-modify-existing-xml-file-with-xmldocument-and-xmlnode-in-c-sharp) see this post... – Trevor Apr 24 '19 at 20:12

0 Answers0