【问题标题】:Need help in reading the data from credentials xml file在从凭证 xml 文件中读取数据时需要帮助
【发布时间】:2010-03-04 15:13:40
【问题描述】:

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

【问题讨论】:

    标签: xml login credentials


    【解决方案1】:
    string username = root.SelectNodes("//username")[0].InnerText; // Rahul1
    username = root.SelectNodes("//username")[1].InnerText; // Rahul2
    username = root.SelectNodes("//username")[2].InnerText; // Rahul3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多