自定义XML配置文件的操作类
来源:互联网 发布:淘宝卖家app官方下载 编辑:程序博客网 时间:2024/05/01 00:02
自定义XML配置文件的操作类
xml配置文件:
<?xml version="1.0"?>
<ModuleSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DataSource>fujian</DataSource>
<Uid>fujian_zyz</Uid>
<Password>fujian_sql_zyz</Password>
<ShowLogin>false</ShowLogin>
</ModuleSettings>
操作类的实现:
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
namespace LTPTOOL
{
#region 配置对象模型类
/// <summary>
/// 李天平
/// 2004.3
/// 配置的modul类(注意相关属性的类型与[XmlElement])
/// use:ModuleSettings settings=ModuleConfig.GetSettings();
/// </summary>
public class ModuleSettings
{
private string datasource;
private string uid;
private string password;
private bool showlogin;
[XmlElement]
public string DataSource
{
set{ datasource=value; }
get{ return datasource; }
}
[XmlElement]
public string Uid
{
set{ uid=value; }
get{ return uid; }
}
[XmlElement]
public string Password
{
set{ password=value; }
get{ return password; }
}
[XmlElement]
public bool ShowLogin
{
set{ showlogin=value; }
get{ return showlogin; }
}
}
#endregion
#region 配置的操作类ModuleConfig
/// <summary>
/// 配置的操作类ModuleConfig。
/// </summary>
public class ModuleConfig
{
public static ModuleSettings GetSettings()
{
ModuleSettings data = null;
XmlSerializer serializer = new XmlSerializer(typeof(ModuleSettings));
try
{
string fileName = "db.config";
FileStream fs = new FileStream(fileName, FileMode.Open);
data = (ModuleSettings)serializer.Deserialize(fs);
fs.Close();
}
catch
{
data = new ModuleSettings();
}
return data;
}
public static void SaveSettings(ModuleSettings data)
{
string fileName = "db.config";
XmlSerializer serializer = new XmlSerializer (typeof(ModuleSettings));
// serialize the object
FileStream fs = new FileStream(fileName, FileMode.Create);
serializer.Serialize(fs, data);
fs.Close();
}
#endregion
}
}
得到配置文件的详细信息;
ModuleSettings settings=ModuleConfig.GetSettings();
txtServer.Text=settings.DataSource;
txtUser.Text=settings.Uid;
txtPass.Text=settings.Password;
保存修改:
settings.DataSource=txtServer.Text;
ModuleConfig.SaveSettings(settings);
对于扩展配置也非常方便,只需修改ModuleSettings的对象模型和ModuleConfig.中的文件名就可以用到任何其他配置文件的操作中。
- 自定义XML配置文件的操作类
- 自定义XML配置文件的操作类
- 自定义XML配置文件的操作类
- c# 操作 xml 文件的 自定义类
- JAVA解析XML配置文件的读取操作
- JAVA解析XML配置文件的读取操作
- properties 和xml配置文件的读取操作
- 【工具类】-Xml配置文件读写操作类
- C#操作XML配置文件
- C#操作XML配置文件
- SpringMVC加载自定义目录下的springmvc.xml配置文件
- web工程读取自定义xml配置文件的方法
- 加载自定义目录下的springmvc.xml配置文件 (2)
- springmvc系列(二)-自定义dispatcherservlet加载的xml配置文件
- web.xml加载自定义配置文件(.xml)
- 编写XML作为配置文件的高级操作库
- 编写XML作为配置文件的高级操作库
- JAVA高手解析XML配置文件的读取操作
- SQl Server2
- static c
- C#程序实现动态调用DLL的研究
- JNI中参数的传递与操作(上)
- 数据库的那些故事
- 自定义XML配置文件的操作类
- 学习记录
- 基于FA526处理器SoC平台的Linux操作系统实现
- myeclipse下的一些环境错误
- 读取数据库中数据
- Android基础之Intent(待完善)
- C#时间间隔计算
- QTP - 17 Working with APIs 与windows API交互
- 同步方法