XMLElement xe1 = xd.CreateElement("Users");
XMLElement xe1Name = xd.CreateElement("UserName");
XMLElement xe1Pass = xd.CreateElement("UserPassWord");
xe1Name.InnerText = "Jack";
xe1Pass.InnerText = "123";
xeRoot.AppendChild(xe1);
xe1.AppendChild(xe1Name);
xe1.AppendChild(xe1Pass);
XMLElement xe2 = xd.CreateElement("Users");
XMLElement xe2Name = xd.CreateElement("UserName");
XMLElement xe2Pass = xd.CreateElement("UserPassWord");
xe2Name.InnerText = "King";
xe2Pass.InnerText = "123";
xeRoot.AppendChild(xe2);
xe2.AppendChild(xe2Name);
xe2.AppendChild(xe2Pass);
xd.Save(Application.StartupPath + "\\Users.XML");
接著在btnOK的click事件中輸入如下代碼﹐作為驗証段﹐當然﹐我並沒有對XML文件中的相關敏感信息加密﹐畢竟只算是一個小的學習總結吧。
DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath + "\\Users.XML");
//DataView dv = new DataVIEw();
//dv = ds.Tables[0].DefaultVIEw;
//dv.Sort = "UserName";
//dv.RowFilter = "UserName ='" + UserName.Text.Trim() + "'";
DataTable dt = ds.Tables[0];
DataRow[] dta = dt.Select("UserName='" + UserName.Text.Trim() + "'");
//this.dataGridVIEw1.DataSource = dv;
if (dta != null && dta.Length > 0 )
{
DataRow dr = dta[0];
string strPwd = (string)dr["UserPassWord"];
if (strPwd == this.UserPwd.Text.Trim())
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("No OK");
}
}
else
{
MessageBox.Show("No this account");
}