需要帮助从凭证xml文件中读取数据

时间:2010-03-04 07:05:07

标签: xml login credentials

我想从凭证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";
    }
}

1 个答案:

答案 0 :(得分:0)

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