2011-01-13 13 views
0

私はログインフォームと登録フォームを作成しようとしています。 私の登録フォームが今では重要にはありません、このコードを使用して作成されたXMLファイルをregistratingにより 、C#は、ログインを確認するためにxmlからデータを取得する必要があります

private void newregisterBtn_Click(object sender, EventArgs e) 
    { 
     if (usernameTxb.Text == null && nameTxb.Text == null && ageTxb.Text == null && countryTxb.Text == null && passwordTxb.Text == null) 
     { 
      usernLbl.ForeColor = Color.Red; 
      nameLbl.ForeColor = Color.Red; 
      ageLbl.ForeColor = Color.Red; 
      countryLbl.ForeColor = Color.Red; 
      passwordLbl.ForeColor = Color.Red; 
// this doesn't work 

     } 
     else 
     { 
      string filename = @"C:\\testxml\\" + usernameTxb.Text + ".xml"; 

      XmlDocument doc = new XmlDocument(); 
      XmlElement root = doc.CreateElement("Login"); 
      XmlElement id = doc.CreateElement("id"); 
      id.SetAttribute("userName", usernameTxb.Text); 
      id.SetAttribute("passWord", passwordTxb.Text); 
      XmlElement name = doc.CreateElement("Name"); 
      name.InnerText = nameTxb.Text; 
      XmlElement age = doc.CreateElement("Age"); 
      age.InnerText = ageTxb.Text; 
      XmlElement Country = doc.CreateElement("Country"); 
      Country.InnerText = countryTxb.Text; 
      id.AppendChild(name); 
      id.AppendChild(age); 
      id.AppendChild(Country); 
      root.AppendChild(id); 
      doc.AppendChild(root); 
      doc.Save(filename); 
      MessageBox.Show("Created SuccesFully!"); 
      this.Close(); 

     } 
    } 

完了し、私の第二の形式(ログインフォーム)に、私は単に「usernameTxb」A」を得ましたpasswordTxb 'は' loginBtn 'と' registerBtn 'です。

ここで、usernameTxbとpasswordTxbがxmlファイルに書き込まれた情報と同じであるかどうかを調べるコードが必要です。 ここにいくつかのXMLコードです。

<Login> 
    <id userName="Username" passWord="password"> 
    <Name>Joshua Maerten</Name> 
    <Age>21</Age> 
    <Country>Belgium</Country> 
    </id> 
</Login> 
+1

Excelent投稿http://stackoverflow.com/questions/4683834/c-sharp-need-to-get-data-from-xml-to-verify-login/17239325#17239325 –

答えて

2

概念的にあなたのXMLに基づいて動作するはずあなたはXMLがこれを解析するためにLINQを使用することができた場合:

public static bool IsValidLogin(string user, string password) 
{ 
    XDocument doc = XDocument.Load("Login.xml"); 

    return doc.Descendants("id") 
       .Where(id => id.Attribute("userName").Value == user 
        && id.Attribute("passWord").Value == password) 
       .Any(); 
} 
+0

問題は、ファイルが""文字列ファイル名= @ "" C:\\ testxml \\ "+ usernameTxb.Text +" .xml "; ""そして、xDocument doc = newの前に置くと、彼は言う:エラー非静的フィールド、メソッド、またはプロパティ 'application.Login.usernameTxにはオブジェクト参照が必要です。私は自分の道を真実にするのを助けてくれるのですが、loginbuttonのコードを教えてください。 –

+0

'' login.xml "'を 'user +"。xml "'に置き換えるだけですが、なぜ各ユーザごとに別々のファイルがあるのでしょうか?あなたのXML構造は既にそれをサポートしています(ログインデータを決してクリアテキストで保管しないでください) – BrokenGlass

+0

私は自分の道を真実にしてくれていますが、 loginbuttonのコードを教えてください。 –

2

Asp Login Page

XmlDocument

CodeBehindPage(.cs) こんにちは苦しんでいる人は、この記事Asp.netとC#でXML文書を使用しているログインページから。 以下を参照してください詳細をすべて明確にします詳細Raffiに感謝します。

関連する問題