Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7933548
  • 博文数量: 124
  • 博客积分: 2880
  • 博客等级: 少校
  • 技术积分: 873
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-16 17:08
文章分类

全部博文(124)

文章存档

2011年(28)

2010年(60)

2009年(36)

我的朋友

分类: C/C++

2009-09-17 09:58:34

C#读写XML配置文件
 

xml文件如下:



 123
 456
 789
 123
 467

利用C#读写代码如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace WindowsApplication1
{
    class XmlOperator
    {
        private string xmlPath;
        private XmlDocument myDc=new XmlDocument();
        ///


        /// 装载配置文件
        ///

        /// 配置文件的路径
        public XmlOperator(string Path)
        {
            xmlPath = Path;
            try
            {
                myDc.Load(xmlPath);
            }
            catch
            {
                MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }
        ///
        /// 将配置文件中的值读入
        ///

        public void GetXmlReader()
        {
            PublicData.DBPath = myDc.SelectSingleNode("system").SelectSingleNode("DBPath").InnerText;
            PublicData.BackPicturePath = myDc.SelectSingleNode("system").SelectSingleNode("BackPicturePath").InnerText;
            PublicData.MIDPath = myDc.SelectSingleNode("system").SelectSingleNode("MIDPath").InnerText;
            PublicData.DBName = myDc.SelectSingleNode("system").SelectSingleNode("DBName").InnerText;
            PublicData.DBPassword = myDc.SelectSingleNode("system").SelectSingleNode("DBPassword").InnerText;
        }
        ///
        /// 将自己的设置写入配置文件
        ///

        ///
        public void SetXmlFile()
        {
            myDc.SelectSingleNode("system").SelectSingleNode("DBPath").InnerText = PublicData.DBPath;
            myDc.SelectSingleNode("system").SelectSingleNode("BackPicturePath").InnerText = PublicData.BackPicturePath;
            myDc.SelectSingleNode("system").SelectSingleNode("MIDPath").InnerText = PublicData.MIDPath;
            myDc.SelectSingleNode("system").SelectSingleNode("DBName").InnerText = PublicData.DBName;
            myDc.SelectSingleNode("system").SelectSingleNode("DBPassword").InnerText = PublicData.DBPassword;
            myDc.Save(xmlPath);
        }
    }
}

阅读(3330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~