Imports System.Xml
Public Class Config
Private xdoc As XmlDocument
Private Shared cfg As Config
Private fileName As String
REM 单例模式
Private Sub New()
cfg = Me
End Sub
REM 保存配置
Public Sub Save()
xdoc.Save(fileName)
End Sub
REM 创建一个配置
Public Shared Function CreateConfig(ByVal filepath As String, Optional ByVal rootName As String = "Root") As Config
cfg = New Config()
cfg.fileName = filepath
cfg.xdoc = New XmlDocument()
cfg.xdoc.AppendChild(cfg.xdoc.CreateXmlDeclaration("1.0", "utf-8", Nothing))
Dim element As XmlElement = cfg.xdoc.AppendChild(cfg.xdoc.CreateElement(rootName))
'cfg.xdoc.Save(filepath)
Return cfg
End Function
REM 载入一个配置
Public Shared Function LoadConfig(ByVal filepath As String) As Config
cfg = New Config()
cfg.fileName = filepath
cfg.xdoc = New XmlDocument()
cfg.xdoc.Load(filepath)
If cfg.xdoc Is Nothing Then
Return Nothing
Else
Return cfg
End If
End Function
REM 返回XMLDocument对象
Public Function GetXDoc()
Return xdoc
End Function
REM 创建新节点
Public Function CreateNode(ByVal name As String, ByVal parent As XmlNode, Optional ByVal namespaceUri As String = Nothing) As XmlNode
Dim result As XmlNode
If namespaceUri Is Nothing Then
result = xdoc.CreateNode(XmlNodeType.Element, name, parent.NamespaceURI)
Else
result = xdoc.CreateNode(XmlNodeType.Element, name, namespaceUri)
End If
parent.AppendChild(result)
Return result
End Function
REM 创建新节点属性
Public Function CreateAttribute(ByVal name As String, Optional value As String = Nothing) As XmlAttribute
Dim result As XmlAttribute = xdoc.CreateAttribute(name)
If (value IsNot Nothing) Then
result.Value = value
End If
Return result
End Function
REM 修改节点的属性名
Public Function ModifyAttribName(ByVal node As XmlNode, ByVal oldAttribName As String, ByVal newAttribName As String, Optional ByVal attribValue As String = Nothing) As Boolean
If node IsNot Nothing Then
Dim element As XmlElement = node
If node.Attributes Is Nothing Then Return False
If node.Attributes.GetNamedItem(oldAttribName) Is Nothing Then
Return False
End If
Dim tempAttribValue As String = GetValueByNodeAndAtrrib(node.Name, oldAttribName)
If attribValue Is Nothing Then
attribValue = tempAttribValue
End If
element.RemoveAttribute(oldAttribName)
element.SetAttribute(newAttribName, attribValue)
Return True
End If
Return False
End Function
REM 修改节点的名字
Public Function ModifyNodeName(ByVal node As XmlNode, ByVal nodeName As String)
If node IsNot Nothing Then
Dim newNode As XmlNode = xdoc.CreateElement(nodeName)
For Each n As XmlNode In node.ChildNodes
newNode.AppendChild(n)
Next
If node.Attributes Is Nothing Then Return False
For Each a As XmlAttribute In node.Attributes
newNode.Attributes.Append(a)
Next
Dim parent As XmlNode = node.ParentNode
parent.RemoveChild(node)
parent.AppendChild(newNode)
Return True
End If
Return False
End Function
REM 设置节点的属性值
Public Function SetValueOfNodeAttrib(ByVal node As XmlNode, ByVal attribName As String, ByVal attribValue As String) As Boolean
If node IsNot Nothing Then
If node.Attributes Is Nothing Then Return False
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
node.Attributes(attribName).Value = attribValue
Return True
End If
Return False
End If
Return False
End Function
REM 遍历,得到XML的指定节点中某个属性的数据。
Public Function GetValueByNodeAndAtrrib(ByVal nodeName As String, ByVal attribName As String, Optional ByVal parent As XmlNode = Nothing) As String
If (parent Is Nothing) Then
For Each node As XmlNode In xdoc
If (node.ChildNodes.Count > 0) Then
Dim result As String = GetValueByNodeAndAtrrib(nodeName, attribName, node)
If (result <> Nothing) Then
Return result
End If
If (node.Name = nodeName) Then
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
Return node.Attributes.GetNamedItem(attribName).Value
End If
End If
Return Nothing
End If
Next
Else
For Each node As XmlNode In parent
If (node.ChildNodes.Count > 0) Then
Dim result As String = GetValueByNodeAndAtrrib(nodeName, attribName, node)
If (result <> Nothing) Then
Return result
End If
End If
If (node.Name = nodeName) Then
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
Return node.Attributes.GetNamedItem(attribName).Value
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function GetValuesByNodeAndAtrrib(ByVal nodeName As String, ByVal attribName As String, Optional ByVal parent As XmlNode = Nothing) As List(Of String)
Dim list As New List(Of String)
If (parent Is Nothing) Then
For Each node As XmlNode In xdoc
If (node.ChildNodes.Count > 0) Then
Dim result As List(Of String) = GetValuesByNodeAndAtrrib(nodeName, attribName, node)
If (result IsNot Nothing) Then
list.AddRange(result)
End If
If (node.Name = nodeName) Then
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
list.Add(node.Attributes.GetNamedItem(attribName).Value)
End If
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
If (node.ChildNodes.Count > 0) Then
Dim result As List(Of String) = GetValuesByNodeAndAtrrib(nodeName, attribName, node)
If (result IsNot Nothing) Then
list.AddRange(result)
End If
End If
If (node.Name = nodeName) Then
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
list.Add(node.Attributes.GetNamedItem(attribName).Value)
End If
End If
Next
Return list
End If
Return list
End Function
REM 根据节点名字查找节点
Public Function FindNodeByName(ByVal name As String, Optional ByVal parent As XmlNode = Nothing) As XmlNode
If parent Is Nothing Then
For Each node As XmlNode In xdoc
Dim result As XmlNode
If node.Name = name Then
Return node
End If
If node.ChildNodes.Count > 0 Then
result = FindNodeByName(name, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
Else
For Each node As XmlNode In parent
Dim result As XmlNode
If node.Name = name Then
Return node
End If
If node.ChildNodes.Count > 0 Then
result = FindNodeByName(name, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function FindNodesByName(ByVal name As String, Optional ByVal parent As XmlNode = Nothing) As List(Of XmlNode)
Dim list As New List(Of XmlNode)
If parent Is Nothing Then
For Each node As XmlNode In xdoc
Dim result As List(Of XmlNode)
If node.Name = name Then
list.Add(node)
End If
If node.ChildNodes.Count > 0 Then
result = FindNodesByName(name, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
Dim result As List(Of XmlNode)
If node.Name = name Then
list.Add(node)
End If
If node.ChildNodes.Count > 0 Then
result = FindNodesByName(name, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
End If
Return list
End Function
REM 根据给出的属性名查找节点
Public Function FindNodeByAttribName(ByVal attribName As String, ByVal attribValue As String, Optional ByVal parent As XmlNode = Nothing) As XmlNode
If parent Is Nothing Then
For Each node As XmlNode In xdoc
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
Return node
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As XmlNode = FindNodeByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
Else
For Each node As XmlNode In parent
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
Return node
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As XmlNode = FindNodeByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function FindNodesByAttribName(ByVal attribName As String, ByVal attribValue As String, Optional ByVal parent As XmlNode = Nothing) As List(Of XmlNode)
Dim list As New List(Of XmlNode)
If parent Is Nothing Then
For Each node As XmlNode In xdoc
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
list.Add(node)
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As List(Of XmlNode) = FindNodesByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
list.Add(node)
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As List(Of XmlNode) = FindNodesByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
End If
Return list
End Function
End Class
Public Class Config
Private xdoc As XmlDocument
Private Shared cfg As Config
Private fileName As String
REM 单例模式
Private Sub New()
cfg = Me
End Sub
REM 保存配置
Public Sub Save()
xdoc.Save(fileName)
End Sub
REM 创建一个配置
Public Shared Function CreateConfig(ByVal filepath As String, Optional ByVal rootName As String = "Root") As Config
cfg = New Config()
cfg.fileName = filepath
cfg.xdoc = New XmlDocument()
cfg.xdoc.AppendChild(cfg.xdoc.CreateXmlDeclaration("1.0", "utf-8", Nothing))
Dim element As XmlElement = cfg.xdoc.AppendChild(cfg.xdoc.CreateElement(rootName))
'cfg.xdoc.Save(filepath)
Return cfg
End Function
REM 载入一个配置
Public Shared Function LoadConfig(ByVal filepath As String) As Config
cfg = New Config()
cfg.fileName = filepath
cfg.xdoc = New XmlDocument()
cfg.xdoc.Load(filepath)
If cfg.xdoc Is Nothing Then
Return Nothing
Else
Return cfg
End If
End Function
REM 返回XMLDocument对象
Public Function GetXDoc()
Return xdoc
End Function
REM 创建新节点
Public Function CreateNode(ByVal name As String, ByVal parent As XmlNode, Optional ByVal namespaceUri As String = Nothing) As XmlNode
Dim result As XmlNode
If namespaceUri Is Nothing Then
result = xdoc.CreateNode(XmlNodeType.Element, name, parent.NamespaceURI)
Else
result = xdoc.CreateNode(XmlNodeType.Element, name, namespaceUri)
End If
parent.AppendChild(result)
Return result
End Function
REM 创建新节点属性
Public Function CreateAttribute(ByVal name As String, Optional value As String = Nothing) As XmlAttribute
Dim result As XmlAttribute = xdoc.CreateAttribute(name)
If (value IsNot Nothing) Then
result.Value = value
End If
Return result
End Function
REM 修改节点的属性名
Public Function ModifyAttribName(ByVal node As XmlNode, ByVal oldAttribName As String, ByVal newAttribName As String, Optional ByVal attribValue As String = Nothing) As Boolean
If node IsNot Nothing Then
Dim element As XmlElement = node
If node.Attributes Is Nothing Then Return False
If node.Attributes.GetNamedItem(oldAttribName) Is Nothing Then
Return False
End If
Dim tempAttribValue As String = GetValueByNodeAndAtrrib(node.Name, oldAttribName)
If attribValue Is Nothing Then
attribValue = tempAttribValue
End If
element.RemoveAttribute(oldAttribName)
element.SetAttribute(newAttribName, attribValue)
Return True
End If
Return False
End Function
REM 修改节点的名字
Public Function ModifyNodeName(ByVal node As XmlNode, ByVal nodeName As String)
If node IsNot Nothing Then
Dim newNode As XmlNode = xdoc.CreateElement(nodeName)
For Each n As XmlNode In node.ChildNodes
newNode.AppendChild(n)
Next
If node.Attributes Is Nothing Then Return False
For Each a As XmlAttribute In node.Attributes
newNode.Attributes.Append(a)
Next
Dim parent As XmlNode = node.ParentNode
parent.RemoveChild(node)
parent.AppendChild(newNode)
Return True
End If
Return False
End Function
REM 设置节点的属性值
Public Function SetValueOfNodeAttrib(ByVal node As XmlNode, ByVal attribName As String, ByVal attribValue As String) As Boolean
If node IsNot Nothing Then
If node.Attributes Is Nothing Then Return False
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
node.Attributes(attribName).Value = attribValue
Return True
End If
Return False
End If
Return False
End Function
REM 遍历,得到XML的指定节点中某个属性的数据。
Public Function GetValueByNodeAndAtrrib(ByVal nodeName As String, ByVal attribName As String, Optional ByVal parent As XmlNode = Nothing) As String
If (parent Is Nothing) Then
For Each node As XmlNode In xdoc
If (node.ChildNodes.Count > 0) Then
Dim result As String = GetValueByNodeAndAtrrib(nodeName, attribName, node)
If (result <> Nothing) Then
Return result
End If
If (node.Name = nodeName) Then
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
Return node.Attributes.GetNamedItem(attribName).Value
End If
End If
Return Nothing
End If
Next
Else
For Each node As XmlNode In parent
If (node.ChildNodes.Count > 0) Then
Dim result As String = GetValueByNodeAndAtrrib(nodeName, attribName, node)
If (result <> Nothing) Then
Return result
End If
End If
If (node.Name = nodeName) Then
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
Return node.Attributes.GetNamedItem(attribName).Value
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function GetValuesByNodeAndAtrrib(ByVal nodeName As String, ByVal attribName As String, Optional ByVal parent As XmlNode = Nothing) As List(Of String)
Dim list As New List(Of String)
If (parent Is Nothing) Then
For Each node As XmlNode In xdoc
If (node.ChildNodes.Count > 0) Then
Dim result As List(Of String) = GetValuesByNodeAndAtrrib(nodeName, attribName, node)
If (result IsNot Nothing) Then
list.AddRange(result)
End If
If (node.Name = nodeName) Then
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
list.Add(node.Attributes.GetNamedItem(attribName).Value)
End If
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
If (node.ChildNodes.Count > 0) Then
Dim result As List(Of String) = GetValuesByNodeAndAtrrib(nodeName, attribName, node)
If (result IsNot Nothing) Then
list.AddRange(result)
End If
End If
If (node.Name = nodeName) Then
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
list.Add(node.Attributes.GetNamedItem(attribName).Value)
End If
End If
Next
Return list
End If
Return list
End Function
REM 根据节点名字查找节点
Public Function FindNodeByName(ByVal name As String, Optional ByVal parent As XmlNode = Nothing) As XmlNode
If parent Is Nothing Then
For Each node As XmlNode In xdoc
Dim result As XmlNode
If node.Name = name Then
Return node
End If
If node.ChildNodes.Count > 0 Then
result = FindNodeByName(name, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
Else
For Each node As XmlNode In parent
Dim result As XmlNode
If node.Name = name Then
Return node
End If
If node.ChildNodes.Count > 0 Then
result = FindNodeByName(name, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function FindNodesByName(ByVal name As String, Optional ByVal parent As XmlNode = Nothing) As List(Of XmlNode)
Dim list As New List(Of XmlNode)
If parent Is Nothing Then
For Each node As XmlNode In xdoc
Dim result As List(Of XmlNode)
If node.Name = name Then
list.Add(node)
End If
If node.ChildNodes.Count > 0 Then
result = FindNodesByName(name, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
Dim result As List(Of XmlNode)
If node.Name = name Then
list.Add(node)
End If
If node.ChildNodes.Count > 0 Then
result = FindNodesByName(name, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
End If
Return list
End Function
REM 根据给出的属性名查找节点
Public Function FindNodeByAttribName(ByVal attribName As String, ByVal attribValue As String, Optional ByVal parent As XmlNode = Nothing) As XmlNode
If parent Is Nothing Then
For Each node As XmlNode In xdoc
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
Return node
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As XmlNode = FindNodeByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
Else
For Each node As XmlNode In parent
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
Return node
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As XmlNode = FindNodeByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
End If
Return Nothing
End Function
Public Function FindNodesByAttribName(ByVal attribName As String, ByVal attribValue As String, Optional ByVal parent As XmlNode = Nothing) As List(Of XmlNode)
Dim list As New List(Of XmlNode)
If parent Is Nothing Then
For Each node As XmlNode In xdoc
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
list.Add(node)
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As List(Of XmlNode) = FindNodesByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
Else
For Each node As XmlNode In parent
If node.Attributes Is Nothing Then Continue For
If node.Attributes.GetNamedItem(attribName) IsNot Nothing Then
If node.Attributes.GetNamedItem(attribName).Value = attribValue Then
list.Add(node)
End If
End If
If node.ChildNodes.Count > 0 Then
Dim result As List(Of XmlNode) = FindNodesByAttribName(attribName, attribValue, node)
If result IsNot Nothing Then
list.AddRange(result)
End If
End If
Next
Return list
End If
Return list
End Function
End Class