最近对于web开发的信息管理的系统的架构兴趣浓厚,对于应用于自定义平台的开发非常感兴趣,最近也自不量力想开发出自己的一套应用平台。避免繁琐而又容易出错的编码工作。平台刚刚起步,具体功能为实现。今天先以自定义config文件为例,讲解描述一下自定义config文件的读取工作。
Platform.config 为基于标准的xml文件的自定义config文件,用于存储平台的各个模块的配置信息。首先包含的就是 数据库的切换模块 config文件信息如下:

<?xml version="1.0" encoding="utf-8" ?>
<Platform>
<DataBase>
<!--使用的数据库-->
<DBApp Name="SqlServer"></DBApp>
<DBList>
<add DBName="Access" DBProvider="" DBConnectionString=""></add>
<add DBName="SqlServer" DBProvider="System.Data.SqlClient" DBConnectionString=""></add>
<add DBName="MySql" DBProvider="" DBConnectionString=""></add>
<add DBName="Oracle" DBProvider="" DBConnectionString=""></add>
<add DBName="DB" DBProvider="" DBConnectionString=""></add>
</DBList>
</DataBase>
<Cache>
</Cache>
</Platform>
读取Platform类如下:
1. 实体类 返回 DBHelper需要的两个参数,Provider 和 ConnectionString

namespace Donet.Configuration
{
public class DBItem
{
private string _DBProvider;
///<summary>
/// 数据库,提供商
///</summary>
public string DBProvider
{
get { return _DBProvider; }
set { _DBProvider = value; }
}
private string _DBConnectionString;
///<summary>
/// 连接字符串
///</summary>
public string DBConnectionString
{
get { return _DBConnectionString; }
set { _DBConnectionString = value; }
}
}
}
2. 文件读取类

namespace Donet.Configuration
{
public static class PlatformConfig
{
public static DBItem GetDBItem(string filePath)
{
DBItem dbItem = new DBItem();
// new一个XMLTextReader实例
XmlTextReader reader = new XmlTextReader(filePath);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
//关闭reader,不然config文件就变成只读的了
reader.Close();
XmlNodeList nodeList = doc.SelectSingleNode("/Platform").ChildNodes;
string dbName = nodeList[0]["DBApp"].Attributes["Name"].Value;
XmlNodeList addList = nodeList[0]["DBList"].ChildNodes;
foreach (XmlNode node in addList)
{
if (node.Attributes["DBName"].Value == dbName)
{
dbItem.DBProvider = node.Attributes["DBProvider"].Value;
dbItem.DBConnectionString = node.Attributes["DBConnectionString"].Value;
}
}
return dbItem;
}
}
}
3.0 调用 方式

Donet.Configuration.PlatformConfig.GetDBItem(Server.MapPath("Platform.config")).DBProvider;
实例图:
晕了不会插图图片。
好了就这些吧,希望有平台开发的前辈指点一下,平台开发。不是那个平台怎么用,而是怎样开发出一个成熟的平台,来应对日新月异的需求。
这个config的管理类我还会继续更新,以达到高质量的代码。欢迎各位前辈指正。谢绝喷子。V