XML4Config

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,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇