XML as INI

Hi!

New to both VB an .NET I have searched the Internet for an easy-to-use example of how to store application/component property settings to a file. As I understand it the common INI-files are not longer supported (in a natural way) by .NET. It is no problem for me using XML instead, but I can not find an example of how to use XML for such purpose or every example I tried seems to be very tricky and circumstanstial. Is it not really an easier way to replace the old "ini-method".

I would be greatful for any suggestions or codesamples.

 

Niklas




Answer this question

XML as INI

  • NeoNmaN

    Hi,

    If it is application configuration settings that you want to store then take a look at the following articles:

    http://odetocode.com/Articles/418.aspx

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

    Regards,

    Vikram

     

     



  • Dotty

    Give this a try.  It should work in the latest version of VB2005EE

    Create a new Form1

    Put this code above the Public Class Form1 secition (very top of the form, above everything)

    Imports System.Data
    Imports System.Xml
    Imports System.Xml.XmlDataDocument
     

    Put these global variables just below the Public Class Form1

    Dim ds As New DataSet(), dr As DataRow, dt As DataTable
     
     

    Now put this within the Form_Load event:

    'Create a new table called MySettings and add two different columns
    dt = New DataTable("MySettings")
    dt.Columns.Add("MyFirstColumn", GetType(String))
    dt.Columns.Add("MySecondColumn", GetType(String))
    'add the columns to the dataset
    ds.Tables.Add(dt)
     
    'Populate some data in the columns
    dr = dt.NewRow()
    dr!MyFirstColumn = "Setting1"
    dr!MySecondColumn="Setting2"
    dt.Rows.Add(dr)
     
    'to save the struture (schema) of this dataset issue following command:
    ds.WriteXmlSchema("MySchema.xml")
    'save the actual data in this dataset.
    ds.WriteXml("MyData.xml")
     

    I will add another post on how to read.


  • Gabriela70026

    In the previous example, bring Form1 up in designer and put two textboxes and a button.

    In the code window, just below the Public Class, add:

    Dim schemafilepath As String = "MySchema.xml"
    Dim datafilepath As String = "MyData.xml"
    Dim TotalRows As Integer, CurrentRow as Integer
     

    Locate the Button1 Click Event and put the following:

    Try
         'get the structure file
         ds.ReadXmlSchema(schemafilepath)
         'get the data file
         ds.ReadXml(datafilepath)
    Catch er As Exception    'if a problem opening file
        Throw (er)
    Finally
         dt = ds.Tables!MySettings
    End Try
     
    TotalRows = dt.Rows.Count
    currentRow = 0
     
    txtbox1.Text = dt.rows(currentrow).Item(0)
    txtbox2.Text = dt.rows(currentrow).Item(1)

     

     


  • Sergey Ponomarev

    Thanks for the answers!

    Using XML and a Dataset seems to be be solution I am looking for. It's very structured and flexible, I think.

    One good thing with XML compared to the old INI method is that XML offers 3 dimensions to store data: Tables, columns and rows. This is perfect if you want to be able to load different definition-settings into your application.

    If you for an example store report definition-settings in a XML file you can create one table for Datasource properties, one table for field properties and a third table for report parameters properties. In the parameters table you then define as many columns for the parameter properties you need (ParamName, Description, DefaultValue). And then you add one row for every parameter the actual report include. This is actually how Reporting Services stores its definition.

     

    Thanks!



  • XML as INI