Read XML File in ASP.Net Demo Example

Leave a Comment
In this tutorial, we have to learn how to read XML file in Asp.Net.

In this demo, we have create one login page and we will check username and password is exists in XML file.

login.xml
<?xml version="1.0" encoding="utf-8" ?>
<student>
  <User>
    <username>kisan</username>
    <password>kisan_password</password>
  </User>
  <User>
    <username>devang</username>
    <password>devang_password</password>
  </User>
</student>

Default.aspx
   <form id="form1" runat="server">
    <div>
    <table>
     <tr>
     <td>
         <asp:Label ID="lbl_username" runat="server" Text="Username"></asp:Label>
     </td>
     <td>
         <asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
     </td>
     </tr>
     <tr>
     <td>
         <asp:Label ID="lbl_password" runat="server" Text="Password"></asp:Label>
     </td>
     <td>
         <asp:TextBox TextMode="Password" ID="txt_password" runat="server"></asp:TextBox>
     </td>
     </tr>
     <tr>
     <td>
       
     </td>
     <td>
         <asp:Button ID="btn_login" runat="server" Text="Log in"
             onclick="btn_login_Click" />
     </td>
     <tr>
       <td colspan="2">
           <asp:Label ID="lbl_error" runat="server" Text="Label"></asp:Label>
       </td>
     </tr>
    </table>
    </div>
    </form>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Web.Security;
namespace XMLDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btn_login_Click(object sender, EventArgs e)
        {
            string CurrentUser = "";
            string CurrentPwd = "";
            bool LoginStatus = false;
            string username = txt_username.Text;
            string pwd = txt_password.Text;
            XmlDocument xmxdoc = new XmlDocument();
            xmxdoc.Load(Server.MapPath("~/login.xml"));
            XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("User");
            foreach (XmlNode xn in xmlnodelist)
            {
                XmlNodeList xmlnl = xn.ChildNodes;
                foreach (XmlNode xmln in xmlnl)
                {
                    if (xmln.Name == "username")
                    {
                        if (xmln.InnerText == username)
                        {
                            CurrentUser = username;
                        }
                    }
                    if (xmln.Name == "password")
                    {
                        if (xmln.InnerText == pwd)
                        {
                            CurrentPwd = pwd;
                        }
                    }
                }
                if ((CurrentUser != "") & (CurrentPwd != ""))
                {
                    LoginStatus = true;
                }
            }
            if (LoginStatus == true)
            {
                FormsAuthentication.RedirectFromLoginPage(username, true);
                Response.Redirect("Default2.aspx");
            }
            else
            {
                lbl_error.Text = "You have not authorized to access this website";
            }
        }
    }
}

Download Source Code

0 comments:

Post a Comment