In the past two days, I have become interested in xml as a database. I have searched for some information and gained some insights. I will record it here. It can be regarded as a summary of learning xml by yourself. The technical content is not very strong, so experts and heroes do not need to read it. hehe....
Without further ado, the most important thing for us programmers is practicality. Below we will share the whole process of generating an xml database by ourselves and then logging in and verifying it.
First, please create a windows project, then drag two TextBoxes from the toolbox with IDs UserName and UserPwd, and then drag out two Buttons with IDs btnOK and btnGen. The Text properties are set to "Verification" and " Establish".
Then add the following code to the click event of btnGen to generate an xml file as a database:
XmlDocument xd = new XmlDocument();
XmlNode xnDec = xd.CreateNode(XmlNodeType.XmlDeclaration, "", "");
XmlElement xeRoot = xd.CreateElement("Users");
xd.AppendChild(xnDec);
xd.AppendChild(xeRoot);
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 ");
Then enter the following code in the click event of btnOK as a verification section. Of course, I did not encrypt the relevant sensitive information in the xml file. After all, it is just a small learning summary.
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");
}
http://www.cnblogs.com/jinliangliu/archive/2007/01/08/614813.html