Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / Visual Basic
Article

User Log using XML Class

Rate me:
Please Sign up or sign in to vote.
1.57/5 (3 votes)
30 Jun 20072 min read 26.8K   308   25   2
User Log for small apps

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
student

Comments and Discussions

 
Questionwhy not a Dataset? Pin
roberto galbiati3-Jul-07 0:27
professionalroberto galbiati3-Jul-07 0:27 
hi Probably you already considered it, but why don't you use a Dataset (with your datatable inside)?

you can manage it in memory, and use SaveXML and LoadXML to persist it in a XML file.

bye
roberto
AnswerRe: why not a Dataset? Pin
vp20004-Jul-07 11:40
vp20004-Jul-07 11:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.