您的位置:首页 > 其它

保存程序配置到xml文件里

2012-03-31 08:53 309 查看
准备:

新建一个文本文档(.txt)修改扩展名为.xml,用记事本打开写入变量,格式如下:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Settings>
<text></text>
<R>255</R>
<G>255</G>
<B>255</B>
</Settings>
</NewDataSet>

定义:

DataSet ds; //来存放数据
string path; //XML文件路径

读取方法:

private void Form1_Activated(object sender, EventArgs e)
{
path = Application.StartupPath+"//Settings.xml";
ds = new DataSet();
ds.ReadXml(path); //ds以表格形式读取xml的数据
textBox1.Text = ds.Tables[0].Rows[0][0].ToString();
textBox1.BackColor =Color.FromArgb(Convert.ToInt16(ds.Tables[0].Rows[0][1])
,Convert.ToInt16(ds.Tables[0].Rows[0][2])
, Convert.ToInt16(ds.Tables[0].Rows[0][3]));
Activated -= new EventHandler(Form1_Activated);
}

保存方法:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ds.Tables[0].Rows[0][0] = textBox1.Text;
ds.Tables[0].Rows[0][1] = textBox1.BackColor.R;
ds.Tables[0].Rows[0][2] = textBox1.BackColor.G;
ds.Tables[0].Rows[0][3] = textBox1.BackColor.B;
ds.WriteXml(path);
}

注意:dataset可读入多个表,在

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Settings>
<text></text>
<R>255</R>
<G>255</G>
<B>255</B>
</Settings>
<Settings2>
<text></text>
<R>255</R>
<G>255</G>
<B>255</B>
</Settings2>
<Settings2>
<text></text>
<R>255</R>
<G>255</G>
<B>255</B>
</Settings2>
</NewDataSet>

中Settings与Settings1是dataset中的两个Table名,Settings1表有两行,text,R,G,B,为列名.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: