Results 1 to 8 of 8

Thread: using xml instead of an ini file

  1. #1

    Thread Starter
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165

    using xml instead of an ini file

    can anyone show me how i can write an xml file kind of like an ini file, and also read from it? please bear in mind that i don't know all that much about how xml files work/are structured. thanks.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    if you need this later then you will get it from me (cus i need to do this for myself)

    but if someone has time and can do this, even better

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Take a look at this thread: http://www.vbforums.com/showthread.p...hreadid=147115

    It basicly gives you the idea on how to put a class or Structure (formerly known as type in VB6) in a XML file... and read from it to a class or structure..
    Or even an array!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4

    Thread Starter
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    hmm, ok, i'll give it a go, thanks

  5. #5
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    This an old thread, but it is the same thing as I need to do. I have worked with reading and writing XML before (for data tables) but not for config/ini type files. I was wondering if there is an easier way than I have been using.

    The link above is dead (probably got archived).
    -Shurijo

  6. #6
    Addicted Member
    Join Date
    Aug 1999
    Posts
    164
    I guess I just got lucky. I found this on the Tutorials thread that was just posted a few minutes ago.

    For anyone doing searches in the archived threads, here it is:

    http://www.devx.com/dotnet/Article/7008

    Upgrade Your INI Files to XML with .NET
    As you upgrade your .NET applications, upgrade those legacy INI files as well, and save yourself some headaches.
    -Shurijo

  7. #7
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    here's a little class i wrote awhile ago to do something similar...

    VB Code:
    1. 'XMLSettings Class
    2. 'Date:  February 10th, 2003
    3. 'Programmer:  Jon Dick
    4. 'Website:  [url]http://www.Net-Lutions.com[/url]
    5. 'Email:    mailto:[email protected]
    6. '
    7. 'Description:
    8. '   I designed this class to easily manage program settings in all of my windows and web applications.  There are
    9. 'other INI XML file classes out there, but their code is more complicated though quite possibly more efficient.  
    10. 'My goal was to make a simple class for this, no frills, just something that works, so here it is.  At the same
    11. 'time, i learned a bit more about working with Datasets and XML, and so that was accomplished.
    12. 'Hopefully someone finds this useful, if not for the actual use, then for at least learning how to work with datasets
    13. 'and XML in a simple, quick, and clean fashion.  Enjoy it!  
    14. '
    15. 'Oh yeah, go ahead and use this code for whatever you like.  It took me an hour at most to make, so Use if freely!
    16.  
    17. Public Class XMLSettings
    18.  
    19.     Public Shared Function Write(ByVal strFileName As String, ByVal strSetting As String, ByVal strValue As String) As Boolean
    20.         'This Function Takes a setting and it's corresponding value and writes it to the specified XML file
    21.         Dim dsSettings As DataSet
    22.         dsSettings = New DataSet()
    23.  
    24.         If System.IO.File.Exists(strFileName) Then
    25.             'If a file already exists, load into the dataset
    26.             dsSettings.ReadXml(strFileName)
    27.         Else
    28.             'No file exists, so Create a new dataset
    29.             dsSettings.DataSetName = "XML_INI"
    30.  
    31.             'Create Table in the dataset
    32.             dsSettings.Tables.Add("Settings")
    33.  
    34.             'Create a new row, and add it to the table
    35.             Dim newRow As DataRow
    36.             newRow = dsSettings.Tables(0).NewRow
    37.             dsSettings.Tables(0).Rows.Add(newRow)
    38.  
    39.             'Dispose of the datarow
    40.             newRow = Nothing
    41.  
    42.         End If
    43.  
    44.         'Now we are ready to make changes to the setting and its value
    45.  
    46.         'Check to see if the setting we want exists yet or not
    47.         If dsSettings.Tables(0).Columns(strSetting) Is Nothing Then
    48.             'Setting doesn't exist, so create a new column for it
    49.             dsSettings.Tables(0).Columns.Add(strSetting)
    50.         End If
    51.  
    52.         'Set the column value
    53.         dsSettings.Tables(0).Rows(0)(strSetting) = strValue
    54.  
    55.         'Write the new changes to the xml file
    56.         dsSettings.WriteXml(strFileName)
    57.  
    58.         'Dispose of the Dataset object immediately
    59.         dsSettings.Dispose()
    60.     End Function
    61.  
    62.     Public Shared Function Read(ByVal strFileName As String, ByVal strSetting As String) As String
    63.         'This Function Reads A setting, returning its value (if it exists) from a specified XML File
    64.         Dim dsSettings As DataSet
    65.         dsSettings = New DataSet()
    66.  
    67.         'Try Reading the XML File.  If it doesn't exist, return a setting value of nothing, since the setting
    68.         'obviously also does not exist
    69.         Try
    70.             dsSettings.ReadXml(strFileName)
    71.         Catch
    72.             Read = ""
    73.         End Try
    74.  
    75.         'If the file does exist, attempt to Read the Column (Setting) Value
    76.         'NOTE:  The settings are always saved in the first row of the first table in the dataset
    77.         Try
    78.             Read = dsSettings.Tables(0).Rows(0)(strSetting)
    79.         Catch
    80.             Read = ""
    81.         End Try
    82.  
    83.         'Dispose Immediately of the dataset
    84.         dsSettings.Dispose()
    85.  
    86.     End Function
    87.     Public Shared Function Delete(ByVal strFileName As String, ByVal strSetting As String) As Boolean
    88.         'This Function Deletes A setting, returning a success boolean value
    89.         Dim dsSettings As DataSet
    90.         dsSettings = New DataSet()
    91.  
    92.         'Try Reading the XML File.  If it doesn't exist, return a setting value of nothing, since the setting
    93.         'obviously also does not exist
    94.         Try
    95.             dsSettings.ReadXml(strFileName)
    96.         Catch
    97.             'File doesn't exist, nothing to delete
    98.             Delete = False
    99.             Exit Function
    100.         End Try
    101.  
    102.         'File Exists, so remove the column (setting) if it exists
    103.         Try
    104.             dsSettings.Tables(0).Columns.Remove(strSetting)
    105.  
    106.             'Write Back to XML File
    107.             dsSettings.WriteXml(strFileName)
    108.  
    109.             Delete = True
    110.         Catch
    111.             'Column Doesn't Exist
    112.             Delete = False
    113.         End Try
    114.  
    115.         'Dispose Immediately of the dataset
    116.         dsSettings.Dispose()
    117.     End Function
    118.  
    119. End Class


    It works pretty good for what it's use is... i use it to store program settings....

  8. #8
    New Member
    Join Date
    Apr 2003
    Location
    Nottingham
    Posts
    14
    Or you could create a Serializable class that encapsulates the settings you want to persist - eg.

    PHP Code:
    <Serializable()> Public Class MySettings
        
    Private m_Setting1 As String
        
    Private m_Setting2 As Integer

        
    Public Property Setting1() As String
            Get
                
    Return m_Setting1
            End Get
            Set
    (ByVal Value As String)
                
    m_Setting1 Value
            End Set
        End Property

        
    Public Property Setting2() As Integer
            Get
                
    Return m_Setting2
            End Get
            Set
    (ByVal Value As Integer)
                
    m_Setting2 Value
            End Set
        End Property
    End 
    Class 
    Then in your application you can do the following :

    PHP Code:
          Imports System.Runtime.Serialization.Formatters.Soap
          Imports System
    .IO

          Dim oSerializer 
    As New SoapFormatter() 
          
    Dim oStream As Stream 
          Dim oSettings 
    as MySettings 'an instance of your settings class 

          oSettings.Setting1 = "Some text" 
          oSettings.Setting2 = 123 

          oStream = File.Create("C:\MySettings.xml") 
          oSerializer.Serialize(oStream, oSettings) 
          oStream.Close() 
    This will create a XML file which is then Deserializable (see the Help). Once you have deserialized the object you can just look at its properties.

    It is also possible to create a Binary file if thats preferable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width