模仿C++中INI文件代码操作习惯的XML配置文件类
namspace xqXmlProfile
class XmlProfileWriter
public XmlProfileWriter(string ProfilePath, string ApplicationName) // 类型初始化
public void BeginWrite() // 调用此方法打开xml文件预写
public bool WriteProfileString(string FieldName, string KeyName, string KeyValue) // 调用此方法进行参数写入,可写多值
public bool EndWrite() // 参数写入文成后需调用此方法以更新xml文件
public Exception ErrMsg // 写入参数失败时可获得Exception,方便调试
class XmlProfileReader
public XmlProfileReader(string ProfilePath) // 类型初始化
public string GetProfileString(string FieldName, string KeyName, string DefaultValue) // 获取特定参数 返回值:string
public int GetProfileInt(string FieldName, string KeyName, int DefaultValue) // 获取特定参数 返回值:int
public DataSet GetAllProfiles() // 获取该xml文件中所有记录参数 返回值:DataSet
项目代码示例:
using System;
using System.Collections.Generic;
using System.Text;
//引用命名空间
using xqXmlProfile;
namespace sample
{
class Program
{
static void Main(string[] args)
{
XmlProfileWriter XPW = new XmlProfileWriter("sample.xml", "TestApp");
XPW.BeginWrite();
//写入单一参数
XPW.WriteProfileString("ParamGroup1", "Param", "this is a test!");
//写入多个参数
XPW.WriteProfileString("ParamGroup2", "Param0", "this is a test!
");
XPW.WriteProfileString("ParamGroup2", "Param1", "10");
XPW.EndWrite();

XmlProfileReader XPR = new XmlProfileReader("sample.xml");
//字符串读取
string return0 = XPR.GetProfileString("ParamGroup1", "Param", "");
string return1 = XPR.GetProfileString("ParamGroup2", "Param0", "this is a test!
");
//整形读取
int return2 = XPR.GetProfileInt("ParamGroup2", "Param1", 0);
Console.WriteLine(string.Format("ParamGroup1 Param : {0}", return0));
Console.WriteLine(string.Format("ParamGroup2 Param0 : {0}", return1));
Console.WriteLine(string.Format("ParamGroup2 Param1 : {0}", return2));
}
}
}运行结果:
生成xml文件:
<?xml version="1.0" standalone="yes"?>
<TestApp>
<ParamGroup1>
<Param>this is a test!</Param>
</ParamGroup1>
<ParamGroup2>
<Param0>this is a test!
</Param0>
<Param1>10</Param1>
</ParamGroup2>
</TestApp>

浙公网安备 33010602011771号