xml|读取xml文件中的内容到HashTable

Imports System.Xml

'''
'''
'''
'''
Public Class Message

Private Shared msg As Hashtable = Nothing

Shared Sub New()
If msg Is Nothing Then
initMessage()
End If
End Sub

'''
''' 取得key对应的value,返回调用者
'''
''' 【xml|读取xml文件中的内容到HashTable】取得的valude对应的key
'''
''' 返回key对应的value
''' 对于不存在的key,返回值为空
Public Shared ReadOnly Property Message(ByVal key As String) As String
Get
'定义返回值,初期化为空(对于不存在的key,返回值为空)
Dim value As String = ""
'如果key在HashTable中存在,那么取得对应的值
If msg.ContainsKey(key) Then
value = https://www.it610.com/article/msg(key).ToString()
End If
Return value
End Get

End Property

'读取Message.xml文件中的内容到HashTable
Private Shared Sub initMessage()

'设置Message.xml文件的路径
Dim filename As String = Application.StartupPath & "/Message.xml"
'定义reader,初期化为nothing
Dim reader As XmlReader = Nothing
Dim key As String = ""
Dim value As String = ""
Try
'初期化reader
reader = XmlReader.Create(filename)
msg = New Hashtable
'循环读取
While reader.Read()
'如果NodeType是XmlNodeType.Element并且该节点有属性
If reader.NodeType = XmlNodeType.Element AndAlso reader.HasAttributes Then
'读取key属性
key = reader.GetAttribute("key")
'如果key属性不为空并且msg没有这个key
If String.Empty.Equals(key) = False AndAlso msg.ContainsKey(key) = False Then
'取得value值
value = https://www.it610.com/article/reader.GetAttribute("value")
'添加到HashTable中
msg.Add(key, value)
End If
End If
End While
Finally
'如果reader不为nothing,关闭reader
If reader IsNot Nothing Then
reader.Close()
End If
End Try
End Sub

End Class

附:Message.xml








    推荐阅读