Validate a User using XML and ASP.Net

Just Follow the steps and create a simple application.. Forget those complex database operations…This code is a  VB.Net version of ASP.Net .

1. Open an asp.net application
2. Add a new XML file , name it as users.xml and enter the following info.

<Authorised>
<user>
<username>asdas</username>
<password>asdasd</password>
</user>
<user>
<username>guest</username>
<password>guest</password>
</user>
</Authorised>

3. Then right click on the xml file and click on Create schema.
4. A new schema called users.xsd will get generated.Dont scare..There is nothing to do with that 
5. Ok..Lets do coding.. Go to the design view and place 2 textbox and a button.Name the textbox as txtuname and txtpwd and name the button as btnconfirm.
6. Here is the main code.

Public Function getdata() As DataSet

Dim strpath As String = Server.MapPath(".")
Dim dsusers As New DataSet
dsusers.ReadXmlSchema(strpath & "\users.xsd")
dsusers.ReadXml(strpath & "\users.xml")
Return dsusers
End Function.

So here is a function that returns a Dataset. This function will read all the data from the xml file users.xml in to the dataset dusers. Server.MapPath(".") means this will point to the root directory.

7. On the button “btnconfirm” click enter the following code

Private Sub btnconfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)handles btnconfirm.Click

Dim ds As DataSet = getdata()
Dim rowuser As DataRow =
ds.Tables("user").Rows.Find(txtuname.Text)
If rowuser Is Nothing Then
Response.Redirect("invalid.aspx")
txtuname.Text = ""
txtpwd.Text = ""
Exit Sub
End If
If txtuname.Text = rowuser.Item("username") And txtpwd.Text = rowuser.Item("password") Then
Response.Write("Success.aspx")
Else
Response.Redirect("invalid.aspx")
End If

End Sub

8. So here the function getdata() will return a dataset and is stored in another dataset object ds.Next we have to check whether the user we have entered is there in the Dataset using the method Rows.Find().If the user doesn’t we can redirect the page to an invalid page that says the message like anything.if the user is present we have to check the password too.