Visual Basic Foro

Programación => Visual Basic .NET / C# => Mensaje iniciado por: softmania en Octubre 27, 2013, 02:29:02 am

Título: Usar XML como un archivo INI
Publicado por: softmania en Octubre 27, 2013, 02:29:02 am
Hola, como lo dice el título una forma de usar XML para guardar configuración.

Código: (VB) [Seleccionar]
  Public Function LeerConfig(ByVal sNombre As String) As String
        Dim sReturn As String = String.Empty
        Dim dsConfig As New DataSet
        If System.IO.File.Exists(Application.StartupPath & "\config.xml") Then
            dsConfig.ReadXml(Application.StartupPath & "\config.xml")
        Else
            dsConfig.Tables.Add("Configuracion")
            dsConfig.Tables(0).Columns.Add("Nombre", GetType(String))
            dsConfig.Tables(0).Columns.Add("Valor", GetType(String))
        End If

        Dim dr() As DataRow = dsConfig.Tables("Configuracion").Select("Nombre = '" & sNombre & "'")
        If dr.Length = 1 Then sReturn = dr(0)("Valor").ToString

        Return sReturn
    End Function

    Public Sub GuadarConfig(ByVal sNombre As String, ByVal sValor As String)
        Dim dsConfig As New DataSet
        If System.IO.File.Exists(Application.StartupPath & "\config.xml") Then
            dsConfig.ReadXml(Application.StartupPath & "\config.xml")
        Else

            dsConfig.Tables.Add("Configuracion")
            dsConfig.Tables(0).Columns.Add("Nombre", GetType(String))
            dsConfig.Tables(0).Columns.Add("Valor", GetType(String))
        End If

        Dim dr() As DataRow = dsConfig.Tables(0).Select("Nombre = '" & sNombre & "'")
        If dr.Length = 1 Then
            dr(0)("Valor") = sValor
        Else
            Dim drConfig As DataRow = dsConfig.Tables("Configuracion").NewRow
            drConfig("Nombre") = sNombre
            drConfig("Valor") = sValor
            dsConfig.Tables("Configuracion").Rows.Add(drConfig)
        End If
        dsConfig.WriteXml(Application.StartupPath & "\config.xml")
    End Sub

SALU2
Título: Re:Usar XML como un archivo INI
Publicado por: E N T E R en Octubre 27, 2013, 02:43:38 pm
Interesante gracias por el aporte.