Imports Sy
stem.Xml Public Class Con
fig Private xdoc As XmlDocument Private Shared cfg As Con
fig 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 CreateCon
fig(ByVal filepath As String,Optional ByVal rootName As String = "Root") As Con
fig cfg = New Con
fig() cfg.fileName = filepath cfg.xdoc = New XmlDocument() cfg.xdoc.AppendChild(cfg.xdoc.CreateXmlDecl
aration("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 LoadCon
fig(ByVal filepath As String) As Con
fig cfg = New Con
fig() 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,namespaceUri) 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.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 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.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,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.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,node) If (result <>
nothing) Then Return result End If End If If (node.Name = nodeName) Then 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,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,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,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.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.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,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,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.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,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.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,node) If result IsNot
nothing Then list.AddRange(result) End If End If Next Return list End If Return list End Function End Class