0

我想从凭证 xml 文件中读取用户名和密码。对于成功登录,它应该转到目标页面,或者对于无效登录,它应该显示无效的用户名或密码。假设我在 credential.xml 文件中没有很多用户名和密码,用户可以动态输入任何用户名和密码的组合。请有人帮助我如何做到这一点......我已经编写了代码但抛出错误“对象引用未设置为对象的实例。” 这是我的 Credentials.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>

aspx.cs 页面后面的代码:

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";
    }
}
4

1 回答 1

0
string username = root.SelectNodes("//username")[0].InnerText; // Rahul1
username = root.SelectNodes("//username")[1].InnerText; // Rahul2
username = root.SelectNodes("//username")[2].InnerText; // Rahul3
于 2010-03-04T07:13:40.350 回答