VB.NET 讀寫XML配置文件


本篇內容來自http://blog.sina.com.cn/s/blog_49d619a301009144.html

 

XML配置文件代替INI(VB.NET)

 

Imports System.Data
Public Class CLSReadXML
    Private servername As String '服務器名
    Private Dbname As String '數據庫名稱
    Private Uid As String '數據庫用戶名
    Private pwd As String '數據庫密碼
    Private DSXml As New DataSet
    Private Xpath As String 'Xml文件路徑
    Sub New(ByVal str As String)
       Xpath = str '獲得xml文件存儲路徑
    End Sub
#Region "屬性用於寫入 和得到xml文件內容的屬性"
    Public Property Getserver()
       Get
           Return servername
       End Get
       Set(ByVal Value)
           servername = Value
       End Set
    End Property
    Public Property Getdbname()
       Get
           Return Dbname
       End Get
       Set(ByVal Value)
           Dbname = Value
       End Set
    End Property
    Public Property Getpwd()
       Get
           Return pwd
       End Get
       Set(ByVal Value)
           pwd = Value
       End Set
    End Property
    Public Property GetdUid()
       Get
           Return Uid
       End Get
       Set(ByVal Value)
           Uid = Value
       End Set
    End Property
#End Region
#Region "方法讀寫xml文件的方法當xml文件不存在時自動創建一xml文件"
   '讀取xml文件內容
    Public Function ReadXml() As Boolean
       Try
           DSXml.ReadXml(Xpath)
           servername = DSXml.Tables(0).Rows(0)("server")
           Dbname = DSXml.Tables(0).Rows(0).Item("Dbname")
           Uid = DSXml.Tables(0).Rows(0).Item("uid")
           pwd = DSXml.Tables(0).Rows(0).Item("pwd")
           Return True
       Catch ex As Exception
           Throw ex
           Return False
       End Try
    End Function
   '如果xml文件不存在的時候創建一xml文件
    Public Function WriteXml() As Boolean
       Dim DT As New DataTable
       Try
           DSXml.Tables.Clear()
           DSXml.Clear()
           '給定table列框架
           DT.Columns.Add("server", GetType(String))
           Dt.Columns.Add("DBname", GetType(String))
           Dt.Columns.Add("uid", GetType(String))
           Dt.Columns.Add("pwd", GetType(String))
           Dim DR As DataRow
           DR = Dt.NewRow
           DR.Item("server") = servername
           DR.Item("DBname") = Dbname
           DR.Item("uid") = Uid
           DR.Item("pwd") = pwd
           Dt.Rows.Add(DR)
           DSXml.Tables.Add(Dt)
           DSXml.AcceptChanges()
           DSXml.WriteXml(Xpath)
           Return True
       Catch ex As Exception
           Throw ex
           Return False
       End Try
    End Function
#End Region
End Class

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM