2010-03-04 99 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