保存程序配置到xml文件里

准备:

新建一个文本文档(.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,为列名.

转载于:https://www.cnblogs.com/yazdao/archive/2012/03/31/2426328.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值