关于通用配置管理模块的思考-续(用XmlDocument 还是DataSet)
曾经写过的那些代码由于各种各样的原因没能继续做下去。这段时间时间比较充裕于是又翻了出来,但是我重新整理代码的时候发现这些代码需要重新实现。
原来的实现是将整个配置文件读到DataSet中,然后逐行扫描保存到Hashtable,然后从Hashtable中根据给定的参数做Key,检索特定的配置字符串。现在想象总感觉这些代码有改造的空间(毕竟一个完整的应用程序很可能会有很多自定义的配置),我完全可以用XmlDocment,然后直接用GetElementsByTagName,至少应该节省了放到DataSet中的空间浪费和从DataSet读到Hashtable的时间浪费。代码是在vs.net 2005实现的。
1'**************************************************************************
2'Author: zhaozhanqiang
3'CreateTime: 2005-11-19
4'ModuleName: SettingsManager
5'Discription: Read certain setting from config file
6'**************************************************************************
7
8Imports System.Xml
9Imports System.IO
10Namespace zzq.DotNet.SettingsManager
11
12 Public Class SettingsManager
13 ''' <summary>
14 ''' Read setting from config file
15 ''' </summary>
16 ''' <param name="strKey">Just a key in config file,retrieve a setting string according to it .</param>
17 ''' <returns>Setting string that can be used</returns>
18 ''' <remarks></remarks>
19 Public Shared Function GetSetting(ByVal strKey As String) As String
20
21 Dim strRootpath As String = String.Empty
22 strRootpath = Directory.GetCurrentDirectory
23 Dim strSeparater As String = Path.DirectorySeparatorChar
24 Dim doc As New XmlDocument
25 doc.Load(strRootpath + ".." + strSeparater + ".." + strSeparater + ".." + strSeparater + "Settings" + strSeparater + "Settings.config")
26
27 Return doc.GetElementsByTagName(strKey)(0).InnerText
28
29 End Function
30
31 End Class
32
33End Namespace
34
这里需要说明的地方是没有用系统文件路径分隔符“\”,这样可能在其他语言环境中造成麻烦,其次是这里没有做任何捕获异常的操作,而是在上层调用的时候处理,这样异常层次不会太深便于跟踪 。其余的代码设计说明可以参考前一篇文章。
2'Author: zhaozhanqiang
3'CreateTime: 2005-11-19
4'ModuleName: SettingsManager
5'Discription: Read certain setting from config file
6'**************************************************************************
7
8Imports System.Xml
9Imports System.IO
10Namespace zzq.DotNet.SettingsManager
11
12 Public Class SettingsManager
13 ''' <summary>
14 ''' Read setting from config file
15 ''' </summary>
16 ''' <param name="strKey">Just a key in config file,retrieve a setting string according to it .</param>
17 ''' <returns>Setting string that can be used</returns>
18 ''' <remarks></remarks>
19 Public Shared Function GetSetting(ByVal strKey As String) As String
20
21 Dim strRootpath As String = String.Empty
22 strRootpath = Directory.GetCurrentDirectory
23 Dim strSeparater As String = Path.DirectorySeparatorChar
24 Dim doc As New XmlDocument
25 doc.Load(strRootpath + ".." + strSeparater + ".." + strSeparater + ".." + strSeparater + "Settings" + strSeparater + "Settings.config")
26
27 Return doc.GetElementsByTagName(strKey)(0).InnerText
28
29 End Function
30
31 End Class
32
33End Namespace
34