[原创]再提.NET使用自定义XML做参数配置文件
WEB开发,尤其是大型项目的开发,其中不乏大名鼎鼎的CMS,XXMS,呵呵.
他们都有个共同的特点,那就是网站配置参数N多.
怎么设置好众多的参数配置,是在项目初期需要规划的一个重点.
总结这几年做WEB开发的一点点积累.个人认为以下方案是比较合理化的,对于以后参数的增加修改带来的一些问题可以得到一定的控制.同样适合WINFORM哈.
运行环境: .NET 2.0
在项目设计过程中,采用模块化设计,所以参数也得独立开,每个模块的参数配置单独存放与一个单独的XML文件中.
原理:利用XML的序列化与反序列化配合实体读取和保存参数设置到XML文件中.
先看XML结构
SystemInfo.xml
配置文件子节点结构
自定义配置文件实体
以下需引用
Imports System.Xml
Imports System.Xml.Serialization
序列化XML文件的读取
XML文件保存
调用参数
以上为全部代码
下面做简单说明
以前自己做项目的时候.只是运用到简单的序列化与反序列化,同样也是在CNBLOG先辈们的手迹中得到的解释.
在运用一段时间后发现很繁琐. 因为我只用到了一层结构.
虽然这样很简单明了.可是在实际开发中,不免存在一些问题,比如:
1、输入值的类型判断
2、输入值是否允许空
3、输入值是否符合指定类型
4、在做UI的时候还需要找到对应的控件,再写相关的说明文字。
等等。。很繁琐。
而新的代码。解决了这些问题,在使用中,只需要调用相应节点的值就OK了。在对于内容解释及类型控制上尤其方便。再也不需要因为更改一个参数的控制,而去在N个HTML代码中定位你的配置参数控件,然后再XXXXX。直接使用XML文件编辑器修改XML文件就OK啦。
小弟第一次发表,欢迎大家批评指正!
他们都有个共同的特点,那就是网站配置参数N多.
怎么设置好众多的参数配置,是在项目初期需要规划的一个重点.
总结这几年做WEB开发的一点点积累.个人认为以下方案是比较合理化的,对于以后参数的增加修改带来的一些问题可以得到一定的控制.同样适合WINFORM哈.
运行环境: .NET 2.0
在项目设计过程中,采用模块化设计,所以参数也得独立开,每个模块的参数配置单独存放与一个单独的XML文件中.
原理:利用XML的序列化与反序列化配合实体读取和保存参数设置到XML文件中.
先看XML结构
SystemInfo.xml
<?xml version="1.0"?>
<SystemInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SiteTitle>
<Title>网站标题</Title>
<Name>SiteTitle</Name>
<Explain>设置网站的标题</Explain>
<Mode>Text</Mode>
<Value>博客园</Value>
<Pattern>^.*$</Pattern>
<Required>False</Required>
</SiteTitle>
<SiteUrl>
<Title>网站地址</Title>
<Name>SiteUrl</Name>
<Explain>设置网站的地址</Explain>
<Mode>Text</Mode>
<Value>http://www.cnblogs.com</Value>
<Pattern>^.*$</Pattern>
<Required>False</Required>
</SiteUrl>
</SystemInfo>
<SystemInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SiteTitle>
<Title>网站标题</Title>
<Name>SiteTitle</Name>
<Explain>设置网站的标题</Explain>
<Mode>Text</Mode>
<Value>博客园</Value>
<Pattern>^.*$</Pattern>
<Required>False</Required>
</SiteTitle>
<SiteUrl>
<Title>网站地址</Title>
<Name>SiteUrl</Name>
<Explain>设置网站的地址</Explain>
<Mode>Text</Mode>
<Value>http://www.cnblogs.com</Value>
<Pattern>^.*$</Pattern>
<Required>False</Required>
</SiteUrl>
</SystemInfo>
配置文件子节点结构
''' <summary>
''' 配置文件结构
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class Item
Private _ColsDisplay As UInt16
Private _isRequired As Boolean
Private _name As String
Private _title As String
Private _explain As String
Private _mode As String
Private _value As String
Private _pattern As String
Private _range As String
Private _rangeHint As String
''' <summary>
''' 节点名称
''' </summary>
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
''' <summary>
''' 节点标题
''' </summary>
Public Property Title() As String
Get
Return Me._title
End Get
Set(ByVal value As String)
Me._title = value
End Set
End Property
''' <summary>
''' 节点说明
''' </summary>
Public Property Explain() As String
Get
Return Me._explain
End Get
Set(ByVal value As String)
Me._explain = value
End Set
End Property
''' <summary>
''' 节点值类型
''' </summary>
Public Property Mode() As String
Get
Return Me._mode
End Get
Set(ByVal value As String)
Me._mode = value
End Set
End Property
''' <summary>
''' 节点值
''' </summary>
Public Property Value() As String
Get
Return Me._value
End Get
Set(ByVal value As String)
Me._value = value
End Set
End Property
''' <summary>
''' 限制格式
''' </summary>
Public Property Pattern() As String
Get
Return Me._pattern
End Get
Set(ByVal value As String)
Me._pattern = value
End Set
End Property
''' <summary>
''' 选择范围
''' </summary>
Public Property Range() As String
Get
Return Me._range
End Get
Set(ByVal value As String)
Me._range = value
End Set
End Property
''' <summary>
''' 选择范围标题
''' </summary>
Public Property RangeHint() As String
Get
Return Me._rangeHint
End Get
Set(ByVal value As String)
Me._rangeHint = value
End Set
End Property
''' <summary>
''' 是否必需
''' </summary>
Public Property IsRequired() As Boolean
Get
Return Me._isRequired
End Get
Set(ByVal value As Boolean)
Me._isRequired = value
End Set
End Property
''' <summary>
''' 列显示
''' </summary>
Public Property ColsDisplay() As UInt16
Get
Return Me._ColsDisplay
End Get
Set(ByVal value As UInt16)
Me._ColsDisplay = value
End Set
End Property
End Class
''' 配置文件结构
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class Item
Private _ColsDisplay As UInt16
Private _isRequired As Boolean
Private _name As String
Private _title As String
Private _explain As String
Private _mode As String
Private _value As String
Private _pattern As String
Private _range As String
Private _rangeHint As String
''' <summary>
''' 节点名称
''' </summary>
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
''' <summary>
''' 节点标题
''' </summary>
Public Property Title() As String
Get
Return Me._title
End Get
Set(ByVal value As String)
Me._title = value
End Set
End Property
''' <summary>
''' 节点说明
''' </summary>
Public Property Explain() As String
Get
Return Me._explain
End Get
Set(ByVal value As String)
Me._explain = value
End Set
End Property
''' <summary>
''' 节点值类型
''' </summary>
Public Property Mode() As String
Get
Return Me._mode
End Get
Set(ByVal value As String)
Me._mode = value
End Set
End Property
''' <summary>
''' 节点值
''' </summary>
Public Property Value() As String
Get
Return Me._value
End Get
Set(ByVal value As String)
Me._value = value
End Set
End Property
''' <summary>
''' 限制格式
''' </summary>
Public Property Pattern() As String
Get
Return Me._pattern
End Get
Set(ByVal value As String)
Me._pattern = value
End Set
End Property
''' <summary>
''' 选择范围
''' </summary>
Public Property Range() As String
Get
Return Me._range
End Get
Set(ByVal value As String)
Me._range = value
End Set
End Property
''' <summary>
''' 选择范围标题
''' </summary>
Public Property RangeHint() As String
Get
Return Me._rangeHint
End Get
Set(ByVal value As String)
Me._rangeHint = value
End Set
End Property
''' <summary>
''' 是否必需
''' </summary>
Public Property IsRequired() As Boolean
Get
Return Me._isRequired
End Get
Set(ByVal value As Boolean)
Me._isRequired = value
End Set
End Property
''' <summary>
''' 列显示
''' </summary>
Public Property ColsDisplay() As UInt16
Get
Return Me._ColsDisplay
End Get
Set(ByVal value As UInt16)
Me._ColsDisplay = value
End Set
End Property
End Class
自定义配置文件实体
''' <summary>
''' 自定义配置文件实体
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class Configs
Private _SiteTitle As Item
Private _SiteUrl As Item
''' <summary>
''' 网站标题
''' </summary>
Public Property SiteTitle() As Item
Get
Return Me._SiteTitle
End Get
Set(ByVal value As Item)
Me._SiteTitle = value
End Set
End Property
''' <summary>
''' 网站地址
''' </summary>
Public Property SiteUrl() As Item
Get
Return Me._SiteUrl
End Get
Set(ByVal value As Item)
Me._SiteUrl = value
End Set
End Property
End Class
''' 自定义配置文件实体
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class Configs
Private _SiteTitle As Item
Private _SiteUrl As Item
''' <summary>
''' 网站标题
''' </summary>
Public Property SiteTitle() As Item
Get
Return Me._SiteTitle
End Get
Set(ByVal value As Item)
Me._SiteTitle = value
End Set
End Property
''' <summary>
''' 网站地址
''' </summary>
Public Property SiteUrl() As Item
Get
Return Me._SiteUrl
End Get
Set(ByVal value As Item)
Me._SiteUrl = value
End Set
End Property
End Class
以下需引用
Imports System.Xml
Imports System.Xml.Serialization
序列化XML文件的读取
''' <summary>
''' 读取序列化XML配置文件内容
''' </summary>
''' <param name="Types">类型</param>
''' <param name="FilePath">配置文件地址,绝对地址</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function Deserialize(ByVal Types As Type, ByVal FilePath As String) As Object
Dim Fs As FileStream = Nothing
Dim Xmls As Object = String.Empty
Fs = New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim Xms As XmlSerializer = New XmlSerializer(Types)
Xmls = Xms.Deserialize(Fs)
Fs.Flush()
Fs.Close()
Return Xmls
End Function
''' 读取序列化XML配置文件内容
''' </summary>
''' <param name="Types">类型</param>
''' <param name="FilePath">配置文件地址,绝对地址</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function Deserialize(ByVal Types As Type, ByVal FilePath As String) As Object
Dim Fs As FileStream = Nothing
Dim Xmls As Object = String.Empty
Fs = New FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim Xms As XmlSerializer = New XmlSerializer(Types)
Xmls = Xms.Deserialize(Fs)
Fs.Flush()
Fs.Close()
Return Xmls
End Function
XML文件保存
''' <summary>
''' 保存序列化XML配置文件内容
''' </summary>
''' <param name="Types">类型</param>
''' <param name="Info">实体</param>
''' <param name="FilePath">配置文件地址,绝对地址</param>
''' <remarks></remarks>
Public Shared Sub SaveConfig(ByVal Types As Type, ByVal Info As Configs, ByVal FilePath As String)
Dim Fs As Stream = New FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Dim Xms As XmlSerializer = New XmlSerializer(Types)
Xms.Serialize(Fs, Info)
Fs.Flush()
Fs.Close()
End Sub
''' 保存序列化XML配置文件内容
''' </summary>
''' <param name="Types">类型</param>
''' <param name="Info">实体</param>
''' <param name="FilePath">配置文件地址,绝对地址</param>
''' <remarks></remarks>
Public Shared Sub SaveConfig(ByVal Types As Type, ByVal Info As Configs, ByVal FilePath As String)
Dim Fs As Stream = New FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
Dim Xms As XmlSerializer = New XmlSerializer(Types)
Xms.Serialize(Fs, Info)
Fs.Flush()
Fs.Close()
End Sub
调用参数
Dim Info As Configs = New Configs
Info = Deserialize(GetType (Configs), "SystemInfo.Xml")
Response.write(Info.SiteTitle.Title)
Response.write(Info.SiteTitle.Value)
Info = Deserialize(GetType (Configs), "SystemInfo.Xml")
Response.write(Info.SiteTitle.Title)
Response.write(Info.SiteTitle.Value)
以上为全部代码
下面做简单说明
以前自己做项目的时候.只是运用到简单的序列化与反序列化,同样也是在CNBLOG先辈们的手迹中得到的解释.
在运用一段时间后发现很繁琐. 因为我只用到了一层结构.
<?xml version="1.0"?>
<SysInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SiteTitle>博客园</SiteTitle>
</SysInfo>
<SysInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SiteTitle>博客园</SiteTitle>
</SysInfo>
虽然这样很简单明了.可是在实际开发中,不免存在一些问题,比如:
1、输入值的类型判断
2、输入值是否允许空
3、输入值是否符合指定类型
4、在做UI的时候还需要找到对应的控件,再写相关的说明文字。
等等。。很繁琐。
而新的代码。解决了这些问题,在使用中,只需要调用相应节点的值就OK了。在对于内容解释及类型控制上尤其方便。再也不需要因为更改一个参数的控制,而去在N个HTML代码中定位你的配置参数控件,然后再XXXXX。直接使用XML文件编辑器修改XML文件就OK啦。
小弟第一次发表,欢迎大家批评指正!