VB.net 读写 XML 的类

转载 2012年03月27日 20:50:30

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

相关文章推荐

VB.NET2005读取XML数据

通过调用CreateDataSetFromXml取得XML数据Imports System.XmlPublic Const FILE_CONFIG = "MZZ.xml" Public Cons...

.net实例:vb.net DataSet 操作xml文件

      说明:在微软ado.net 2.0版本中,还不能把整个xml文件映射到DataSet集中。也就是说,XML装入到DataSet时,总是能成功,但是在修改了数据保存时,DataSet始终按默...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

VB.net学习笔记(二十一)XML流的读写

System.Xml名称空间支持XML的处理,为了生成和浏览XML文档,使用两种访问方式: 一、读写方式     (一)基于流的方式     特点:速度快,可连续读写,缺点不能作为节点、元素等进...

VB.net读写XML文件源代码

  • 2011年12月31日 11:26
  • 103KB
  • 下载

vb.net 教程 4-8 文本文件读写 2

继续上一节:

USB接口ID卡读卡器oem软件/读写卡测试程序及源代码/VB.net源代码

  详细代码如下:(如有疑问可13826029618进行相关提问解答,或荣士电子页面查询) Public Class Form1    Inherits System.Windows.Forms.Fo...

USB接口IC读写器oem软件_AB密码完整解决方案/读写卡测试程序及源代码/VB.NET2003源代码

界面图     详细代码  Public Class Form1    Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 "...

vb.net 教程 4-8 文本文件读写 3

继续上一节,继续读写代码的编写。 为了简化操作,所有的Encoding均采用Encoding.Default,获得较好的兼容。 读取方法3; 直接使用StreamReader(   参数1:...

VB.NET多线程文件读写

现在,流行的下载工具都使用了多线程的方式,这样比单线程下载快了很多。   不过那些通常都不是用VB.NET写的,网上可以找到的这方面的资料也比较少。自己琢磨了几天,同时也借助于网友的帮助,解决了这...
  • robake
  • robake
  • 2012年02月29日 10:06
  • 643
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB.net 读写 XML 的类
举报原因:
原因补充:

(最多只允许输入30个字)