VB.net 读写 XML 的类

VB.net 读写 XML 的类...
'使用起来很简单,支持多级项目,级间使用'/'分割。
'厄,Bug 还是有的,但对正常操作没有大的影响,
'如果有什么大的 Bug 或改进建议,请联系我...
Imports System.Xml

Public Class Xml_ClsClass Xml_Cls
    Private XmlDoc As XmlDocument
    Private XmlFile As String

    Public ReadOnly Property XmlFileName()Property XmlFileName() As String
        Get
            Return XmlFile
        End Get
    End Property

    Public ReadOnly Property XmlText()Property XmlText() As String
        Get
            Return XmlDoc.InnerXml
        End Get
    End Property

    Sub New()Sub New(ByVal FileName As String, Optional ByVal CreateNew As Boolean = True, Optional ByVal Root As String = "XML", Optional ByRef IsOK As Boolean = False)
        IsOK = False
        XmlFile = ""

        Dim reader As System.Xml.XmlReader = Nothing
        Try
            reader = New System.Xml.XmlTextReader(FileName)
            reader.Read()
        Catch ex As Exception
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
            Debug.Print("New - " & ex.Message)
            If Not Create(FileName, Root) Then
                Return
            End If
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
        IsOK = True
        XmlFile = FileName
        XmlDoc = New XmlDocument
        XmlDoc.Load(XmlFile)
    End Sub

    Public Function Create()Function Create(ByVal FileName As String, Optional ByVal Root As String = "XML") As Boolean
        Dim NewXML As XmlTextWriter = Nothing

        Try
            NewXML = New XmlTextWriter(FileName, Nothing)

            NewXML.Formatting = Formatting.Indented
            NewXML.WriteStartDocument()
            NewXML.WriteComment(My.Application.Info.AssemblyName & " Settings")

            NewXML.WriteStartElement(Root)

            NewXML.WriteAttributeString("Powered", "Null")

            NewXML.WriteEndElement()
            NewXML.WriteEndDocument()

            NewXML.Flush()
            NewXML.Close()
        Catch ex As Exception
            Debug.Print("Create - " & ex.Message)
            Return False
        Finally
            If NewXML IsNot Nothing Then
                NewXML.Close()
                NewXML = Nothing
            End If

        End Try

        Return True
    End Function

    Public Function Save()Function Save(ByVal aSection As String, ByVal aKey As String, ByVal aValue As String) As Boolean
        Dim Paths() As String
        Dim n As Integer

        Dim Node, Node2 As XmlNode
        Dim Ele As XmlElement

        While Strings.Left(aSection, 1) = "/"
            aSection = Strings.Mid(aSection, 2)
        End While

        '段名是否为空
        If aSection = "" Then
            XmlDoc.DocumentElement.RemoveAll()
        Else

            Paths = Strings.Split(aSection, "/")
            Try
                Node = XmlDoc.DocumentElement.SelectSingleNode(Paths(n))

                If Node Is Nothing Then
                    Ele = XmlDoc.CreateElement(Paths(n))
                    Node = XmlDoc.DocumentElement.AppendChild(Ele)
                End If

                For n = 1 To Paths.Length - 1
                    If Paths(n) = "" Then Continue For

                    Node2 = Node.SelectSingleNode(Paths(n))
                    If Node2 Is Nothing Then
                        Ele = XmlDoc.CreateElement(Paths(n))
                        Node2 = Node.AppendChild(Ele)
                    End If
                    Node = Node2
                Next
                '键名是否为空
                If aKey = "" Then
                    Node.RemoveAll()
                Else
                    Ele = Node.Item(aKey)

                    If Ele Is Nothing Then
                        Ele = XmlDoc.CreateElement(aKey)
                        Node.AppendChild(Ele)
                    End If
                    '值是否为空
                    If aValue = "" Then
                        Node.RemoveChild(Ele)
                    Else
                        Ele.InnerText = aValue
                    End If
                End If
            Catch ex As Exception
                Debug.Print(ex.Message)
                Return False
            End Try
        End If

        XmlDoc.Save(XmlFile)
        Return True
    End Function

    Public Function Read()Function Read(ByVal aSection As String, ByVal aKey As String, Optional ByVal aDefaultValue As String = "") As String
        Dim Node As XmlNode
        Node = (XmlDoc.DocumentElement).SelectSingleNode(aSection & "/" & aKey)
        If Node Is Nothing Then Return aDefaultValue
        Return Node.InnerText
    End Function
End Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值