简单的数据库信息xml文件结构如下:
<?
xml version="1.0" encoding="gb2312"
?>
< server >
< IP > . </ IP >
< Uid > sa </ Uid >
< Pwd > system </ Pwd >
</ server >
< server >
< IP > . </ IP >
< Uid > sa </ Uid >
< Pwd > system </ Pwd >
</ server >
登录窗口的数据库连接信息的界面如下:
当在运行程序时打开了登录的窗体,在窗体的Load事件里直接读取xml数据库连接信息,代码如下:
private
void
LoginFrm_Load(
object
sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("../../ServerInfor.xml");
XmlNode server;
server = doc["server"];
this.tbxIP.Text = server["IP"].InnerText.ToString();
this.tbxUserIP.Text = server["Uid"].InnerText.ToString();
this.tbxIPpass.Text = server["Pwd"].InnerText.ToString();
}
{
XmlDocument doc = new XmlDocument();
doc.Load("../../ServerInfor.xml");
XmlNode server;
server = doc["server"];
this.tbxIP.Text = server["IP"].InnerText.ToString();
this.tbxUserIP.Text = server["Uid"].InnerText.ToString();
this.tbxIPpass.Text = server["Pwd"].InnerText.ToString();
}
但在输入了正确的登录信息后,可点击"登录"按钮,在单击事件里可以对譔xml文件的编辑,写入譔次登录时的数据库连接信息,代码如下:
/**/
/// <summary>
/// 用户进行登录的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click( object sender, System.EventArgs e)
{
ThreadStart threadStart;
Thread thread;
try
{
threadStart=new ThreadStart(Login);
thread=new Thread(threadStart);
thread.Start();
//
XmlTextWriter write = new XmlTextWriter("../../ServerInfor.xml",System.Text.Encoding.Default);
write.Formatting = Formatting.Indented;
write.WriteStartDocument();
write.WriteStartElement("server");
write.WriteElementString("IP",this.tbxIP.Text.Trim().ToString());
write.WriteElementString("Uid",this.tbxUserIP.Text.Trim().ToString());
write.WriteElementString("Pwd",this.tbxIPpass.Text.Trim().ToString());
write.WriteEndElement();
write.WriteEndDocument();
write.Flush();
write.Close();
}
catch
{
MessageBox.Show("启动登录线程失败!");
return;
}
}
/// 用户进行登录的按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click( object sender, System.EventArgs e)
{
ThreadStart threadStart;
Thread thread;
try
{
threadStart=new ThreadStart(Login);
thread=new Thread(threadStart);
thread.Start();
//
XmlTextWriter write = new XmlTextWriter("../../ServerInfor.xml",System.Text.Encoding.Default);
write.Formatting = Formatting.Indented;
write.WriteStartDocument();
write.WriteStartElement("server");
write.WriteElementString("IP",this.tbxIP.Text.Trim().ToString());
write.WriteElementString("Uid",this.tbxUserIP.Text.Trim().ToString());
write.WriteElementString("Pwd",this.tbxIPpass.Text.Trim().ToString());
write.WriteEndElement();
write.WriteEndDocument();
write.Flush();
write.Close();
}
catch
{
MessageBox.Show("启动登录线程失败!");
return;
}
}
微软的XML可视化编辑器:XML Notepad 2007 :
XML Notepad 2007 是微软提供的一款可视化的XML浏览和编辑小工具,直接可以在树形节点中进行拖放操作,输出默认的XSL等;在性能方面也非常好,据微软介绍,加载3M大小的XML文件,只需要一秒钟的时间。
下载地址:
http://www.microsoft.com/downloads/details.aspx?familyid=72D6AA49-787D-4118-BA5F-4F30FE913628&displaylang=en