zoukankan
html css js c++ java
读写类似web.config的xml格式文件
'
读写类似web.config的xml格式文件
Public
Class Config
Class
Config
私有成员变量
#Region
"私有成员变量"
'
配置文件路径
Private
sPath
As
String
'
配置文件对象(xml格式)
Private
xDoc
As
Xml.XmlDocument
#End Region
构造函数
#Region
"构造函数"
'
默认构造函数(取得当前目录下的app.config配置)
Public
Sub New()
Sub
New
()
Dim
assemblyFilePath
As
String
=
System.Reflection.Assembly.GetExecutingAssembly().Location
Dim
assemblyDirPath
As
String
=
System.IO.Path.GetDirectoryName(assemblyFilePath)
Dim
configFilePath
As
String
=
assemblyDirPath
&
"
\app.config
"
sPath
=
configFilePath
xDoc
=
New
XmlDocument
xDoc.Load(sPath)
End Sub
'
用户指定配置文件的构造函数
Public
Sub New()
Sub
New
(
ByVal
s_Path
As
String
)
sPath
=
s_Path
xDoc
=
New
XmlDocument
xDoc.Load(sPath)
End Sub
#End Region
公共接口
#Region
"公共接口"
'
'' -----------------------------------------------------------------------------
'
'' <summary>
'
'' 读取属性
'
'' </summary>
'
'' <param name="keyName">属性名称</param>
'
'' <returns>属性值</returns>
'
'' -----------------------------------------------------------------------------
Public
Function GetValue()
Function
GetValue(
ByVal
keyName
As
String
)
As
String
Dim
str
As
String
=
""
Dim
xElement
As
XmlElement
=
getElement(keyName)
If
Not
xElement
Is
Nothing
Then
str
=
xElement.GetAttribute(
"
value
"
)
End
If
Return
str
End Function
'
'' -----------------------------------------------------------------------------
'
'' <summary>
'
'' 设置属性
'
'' </summary>
'
'' <param name="keyName">属性名称</param>
'
'' <param name="keyValue">属性值</param>
'
'' <param name="bCreate">新增标志(为True的时候当属性不存在时则新增这个属性,默认为False)</param>
'
'' <returns>成功返回True,反之为False</returns>
'
'' -----------------------------------------------------------------------------
Public
Function SetValue()
Function
SetValue(
ByVal
keyName
As
String
,
ByVal
keyValue
As
String
,
Optional
ByVal
bCreate
As
Boolean
=
False
)
As
Boolean
Dim
flag
As
Boolean
=
True
Try
Dim
xElement
As
XmlElement
=
getElement(keyName)
If
Not
xElement
Is
Nothing
Then
xElement.SetAttribute(
"
value
"
, keyValue)
ElseIf
bCreate
Then
Dim
xNode
As
XmlNode
=
getXmlNode()
xElement
=
xDoc.CreateElement(
"
add
"
)
xElement.SetAttribute(
"
key
"
, keyName)
xElement.SetAttribute(
"
value
"
, keyValue)
xNode.AppendChild(xElement)
End
If
xDoc.Save(sPath)
Catch
flag
=
False
End
Try
Return
flag
End Function
#End Region
私有成员
#Region
"私有成员"
'
取得单个元素
Private
Function getElement()
Function
getElement(
ByVal
elemName
As
String
)
As
XmlElement
Dim
xElement
As
XmlElement
Try
Dim
node
As
XmlNode
=
getXmlNode()
xElement
=
CType
(node.SelectSingleNode((
"
//add[@key='
"
&
elemName
&
"
']
"
)), XmlElement)
Catch
xElement
=
Nothing
End
Try
Return
xElement
End Function
'
取得节点
Private
Function getXmlNode()
Function
getXmlNode()
As
XmlNode
Return
xDoc.SelectSingleNode(
"
//appSettings
"
)
End Function
#End Region
End Class
查看全文
相关阅读:
P1440 求m区间内的最小值
P1569 Generic Cow Protests
P3252 [JLOI2012]树
P3009 [USACO11JAN]Profits S
<二分查找+双指针+前缀和>解决子数组和排序后的区间和
常见算法技巧之——双指针思想
设计模式——单例模式
操作系统实验——读者写者模型(写优先)
操作系统——内存管理学习笔记
Altium Designer 16下载与安装教程
原文地址:https://www.cnblogs.com/freemantc/p/304800.html
最新文章
汇编语言-端口(port)
汇编语言-int指令-1-
汇编语言-中断-1
标志寄存器
CALL和RET指令
windows-进程相关操作
初步利用CE分析游戏人物结构
windows-进程线程
卸载dll
2020年最后一天,祝大家新年快乐!
热门文章
ES快速开发,ElasticsearchRestTemplate基本使用以及ELK快速部署
SQL学习笔记系列(六)视图的讲解
SQL学习笔记系列(五)TCL语言的学习
SQL学习笔记系列(四)DDL语言的学习
SQL学习笔记系列(三)DML语言的学习
P1074 [NOIP2009 提高组] 靶形数独
U147730 Get Your Wish
#10022. 「一本通 1.3 练习 1」埃及分数(迭代加深搜索)
P5414 [YNOI2019] 排序
P2029 跳舞
Copyright © 2011-2022 走看看