Reputation: 37
so i am making a login application with a register form but i need some help. i want to save all the account info in xml which i kinda can do, i've created a form where i can save and load 1 user to xml use savefiledialog, here's the code:
The xml Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
namespace Login_Form
{
public class xmlSave
{
public static void SaveData(object IClass, string filename)
{
StreamWriter writer = null;
try
{
XmlSerializer xml = new XmlSerializer((IClass.GetType()));
writer = new StreamWriter(filename);
xml.Serialize(writer, IClass);
}
finally
{
if (writer != null)
writer.Close();
writer = null;
}
}
}
public class xmlLoad<T>
{
public static Type type;
public xmlLoad()
{
type = typeof(T);
}
public T LoadData(string filename)
{
T result;
XmlSerializer xml = new XmlSerializer(type);
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
result = (T)xml.Deserialize(fs);
fs.Close();
return result;
}
}
}
and here is the save and load code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
namespace Login_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog.InitialDirectory = @"C:\Users\Felix\Documents\visual studio 2013\Projects\Login Form\Login Form\bin\Debug";
saveFileDialog.Filter = "xml Files (*.xml)|*.xml";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
User user = new User();
user.FName = textBox1.Text;
user.LName = textBox2.Text;
user.Username = textBox3.Text;
user.Email = textBox4.Text;
user.Password = textBox5.Text;
xmlSave.SaveData(user, saveFileDialog.FileName);
}
}
private void Form1_Load(object sender, EventArgs e)
{
User user = new User();
xmlLoad<User> loadUser = new xmlLoad<User>();
user = loadUser.LoadData("test.xml");
textBox1.Text = user.FName;
textBox2.Text = user.LName;
textBox3.Text = user.Username;
textBox4.Text = user.Email;
textBox5.Text = user.Password;
}
}
public class User
{
private string fName;
private string lName;
private string username;
private string email;
private 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 Email
{
get { return email; }
set { email = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
}
this allows me to save the info from 5 textboxes into a xml file but only 1 user, i want to know how to save multiple users and also how to use this as a login system(no servers, only local for now), and how to load the right info in the main application, for example if im logged in to user 1 i want that users info displayd and the same with other users. im sorry if i explaind myself badly.
Upvotes: 1
Views: 4808
Reputation: 928
your answer.
I have saved user info in xml and loaded it on form load.
while saving give file name as test.xml
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
namespace Login_Form
{
public partial class Form1 : Form
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
saveFileDialog.Filter = "xml Files (*.xml)|*.xml";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (File.Exists(saveFileDialog.FileName))
File.Delete(saveFileDialog.FileName);
User user = new User();
user.FName = textBox1.Text;
user.LName = textBox2.Text;
user.Username = textBox3.Text;
user.Email = textBox4.Text;
user.Password = textBox5.Text;
XmlWriter xmlWriter = XmlWriter.Create(saveFileDialog.FileName);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("UserInfo");
xmlWriter.WriteStartElement("FName");
xmlWriter.WriteString(user.FName);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("LName");
xmlWriter.WriteString(user.LName);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Username");
xmlWriter.WriteString(user.Username);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Email");
xmlWriter.WriteString(user.Email);
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("Password");
xmlWriter.WriteString(user.Password);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
User user = new User();
string filePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
filePath += "\\test.xml";
if (File.Exists(filePath))
{
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode VersionNode = null;
VersionNode = doc.SelectSingleNode("UserInfo");
if (null == VersionNode) return;
user.FName = VersionNode.SelectSingleNode("FName").InnerText;
user.LName = VersionNode.SelectSingleNode("LName").InnerText;
user.Username = VersionNode.SelectSingleNode("Username").InnerText;
user.Email = VersionNode.SelectSingleNode("Email").InnerText;
user.Password = VersionNode.SelectSingleNode("Password").InnerText;
textBox1.Text = user.FName;
textBox2.Text = user.LName;
textBox3.Text = user.Username;
textBox4.Text = user.Email;
textBox5.Text = user.Password;
}
}
}
public class User
{
private string fName;
private string lName;
private string username;
private string email;
private 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 Email
{
get { return email; }
set { email = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
}
}
Upvotes: 2
Reputation: 1631
As mentioned above, the best way is using SQL or at least store all users in one xml file. But if you actually need to store all users information in separated files you can create some directory for users info files only and every time you are logging in or getting some user information you should iterate throw all files and deserialize each one.
something like that:
string[] files = Directory.GetFiles('your user storage directory');
foreach (string file in files) {
user = loadUser.loadData(file);
//your logic goes here
}
Upvotes: 0
Reputation: 11
The best way i think if you use a database if you don't need complex Db, I think SQLite is perfect for you.
My suggest , if you want store the data in xml you must save the whole UserList or you can save the XML files to the users temp folder Path.GetTempPath() but it wokrks only if the users login with different Windows user.
Upvotes: 1