VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 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
复制代码

 


相关教程