Introduction
I was ask to create a small application and to use xml as the database for saving user info. Using Access or SQL would been very easy but using xml I had no idea. I created a simple class to write xml, update and add to it.
Using the code
The class has a couple of methods and properties that are very usefull.
Here is how to use it:
How to use class
1. Create the xml user log file
Sample:
Dim log As New UserLogXml
log.FirstNameElement = "Admin"
log.LastNameElement = "admin1"
log.PasswordElement = "Password2007"
log.EmailElement = "myemail@domain.com"
log.CreateUserFile("C:\WebUsers.xml")
'This can use using textbox.text, log.emailelement=txtEmail.Text
Result:
<?xml version="1.0" encoding="utf-8" ?>
- <Users>
- <User>
<FirstName>Admin</FirstName>
<LastName>admin1</LastName>
<Password>Nv2qrCNpQHfRCJqqQKX+wYsB3cQJVf42MzDEYr9FuY8=</Password>
<Email>myemail@domain.com</Email>
<MemberSince>6/30/2007</MemberSince>
</User>
</Users>
2. Add user to file.
Sample:
Dim applog As New UserLogXml
'This funtion will use TribleDES to encrypt the password
applog.AddUserTDS(Me.txtLog.Text, Me.txtFirstname.Text, Me.txtLastName.Text, Me.txtpassword.Text, Me.txtemail.Text)
The class has 2 functions to add users. The only differences is how the password is encrypted.
The second function will use HASH256 to encrypt.
3.Delete User
Sample:
Dim remove As New UserLogXml
Try
'remove user-firstname
Dim RemoUser As Boolean = remove.RemoveUser2(Me.txtDeleteFirstname.Text, Me.txtDeletePassword.Text, Me.txtLog.Text)
If RemoUser = True Then
MessageBox.Show("User was deleted")
Else
MessageBox.Show("User can't be deleted-not found")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Here you are deleting base on first name and password, passing this both and the location of the xml file. The function will return true or false.
'The other option to delete is remove.RemoveUser-this works the same way but passing firstname and email address.
4. Change Password
Sample:
Dim changepwd As New UserLogXml()
Dim result As Boolean = changepwd.ChangePassword(Me.txtname.Text, Me.txtoldpwd.Text, Me.txtnewpwd.Text, Me.txtsaveto.Text)
If result = True Then
MessageBox.Show("pwd change")
DB.LoadGrid(Me.txtsaveto.Text, Me.DataGridView1)
Else
MessageBox.Show("pwd was not change")
End If
'just pass firstname,new password, old password and the xml path.
'currenlty the new password will be encrypt using hash
5.Optional-this sub add first name and email to a comboBox
Dim loadComboBoxes As New UserLogXml
loadComboBoxes.PopComboBox(Me.txtLog.Text, Me.ComboBox1, Me.ComboBox2)
just pass xml path, add a combobox to your form pass name for firstname,
second for email.
This class allows you to change the xml Tags to what ever you want.
By default all are:
_Root = "Users"
_User = "User"
_FirstName = "FirstName"
_LastName = "LastName"
_Password = "Password"
_email = "Email"
encode = Encoding.UTF8
_MemberSinceDateTag = "MemberSince"
_MemberSinceDateElement = Date.Today
This is class is very simple but it does a good job for small projects.
For beginners like me.
You can change the values for the key and Iv, keep it 24 bytes,8bytes.
This sample might give others a idea to make this class better.