2010-03-04 106 views
0

我想从凭据xml文件读取用户名和密码。为了成功登录它应该去目的地页面或无效登录它应该显示无效的用户名或密码。假设我在credential.xml文件中有许多用户名和密码,用户可以动态输入组合用户名和密码中的任何一个。请有人帮助我如何做到这一点...我写了代码,但抛出错误“对象引用未设置为对象的实例。” 这是我Credentials.xml aspx.cs页面背后需要帮助从凭据xml文件读取数据

<logininfo> 
    <crendential> 
      <username>Rahul1</username> 
      <password>124356</password> 
    </crendential> 
    <crendential> 
       <username>Rahul2</username> 
       <password>654321</password> 
    </crendential> 
    <crendential> 
       <username>Rahul3</username> 
       <password>852741</password> 
    </crendential> 
</logininfo> 

代码:

protected void BtnLogin_Click(object sender, EventArgs e) 
{ 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(Server.MapPath("Credentials.xml")); 
    XmlNode root=doc.DocumentElement; 
    string username = root.SelectSingleNode("username").ChildNodes[0].Value; 
    string password = root.SelectSingleNode("password").ChildNodes[0].Value; 

    string user = TxtUserName.Text.Trim(); 
    string pass = TxtPassword.Text.Trim(); 

    if ((user == username) && (pass == password)) 
    { 
     Session["User"] = username; 
     Response.Redirect("destinationpage.aspx"); 
    } 
    else 
    { 
     lblError.Text="Invalid userid or password"; 
    } 
} 

回答

0
string username = root.SelectNodes("//username")[0].InnerText; // Rahul1 
username = root.SelectNodes("//username")[1].InnerText; // Rahul2 
username = root.SelectNodes("//username")[2].InnerText; // Rahul3