Link to home
Start Free TrialLog in
Avatar of mindweaver
mindweaver

asked on

DataSet/DataTable to XML. Better output

Hey, I have a win application that I want to save settings for. I use  a xml file for this. The output I'm after is this:

<settings>
  <connectionsettings>
    <driver>DRIVERNAME</driver>
    <server>SERVERNAME</server>
    <database>DATABASENAME</database>
    <username>USERNAME</username>
    <password>PASSWORD</password>
    <option>TYPE</option>
  </connectionsettings>
</settings>

But I keep getting:

<settings>
  <usersettings>
    <username>MindWeaver</username>
  </usersettings>
  <connectionsettings>
    <driver>DRIVERNAME</driver>
  </connectionsettings>
  <connectionsettings>
    <server>SERVERNAME</server>
  </connectionsettings>
  <connectionsettings>
    <database>DATABASENAME</database>
  </connectionsettings>
  <connectionsettings>
    <username>USERNAME</username>
  </connectionsettings>
  <connectionsettings>
    <password>PASSWORD</password>
  </connectionsettings>
  <connectionsettings>
    <option>TYPE</option>
  </connectionsettings>
</settings>

The teqnique I'm using is this:

                  string filepath = "settings.xml";
                  DataSet ds = new DataSet("settings");

                  if(File.Exists(filepath))
                  {
                        // Read the XML document into the DataSet.
                        ds.ReadXml(filepath);

                        try
                        {
                              DataTable dt = new DataTable("connectionsettings");

                              DataColumn dc1 = new DataColumn("driver", Type.GetType("System.String"));
                              dt.Columns.Add(dc1);
                              ds.Tables.Add(dt);
                              DataRow newRow1;
                              newRow1 = ds.Tables["connectionsettings"].NewRow();
                              newRow1["driver"] = txtDriver.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow1);

                              DataColumn dc2 = new DataColumn("server", Type.GetType("System.String"));
                              dt.Columns.Add(dc2);
                              DataRow newRow2;
                              newRow2 = ds.Tables["connectionsettings"].NewRow();
                              newRow2["server"] = txtServer.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow2);

                              DataColumn dc3 = new DataColumn("database", Type.GetType("System.String"));
                              dt.Columns.Add(dc3);
                              DataRow newRow3;
                              newRow3 = ds.Tables["connectionsettings"].NewRow();
                              newRow3["database"] = txtDatabase.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow3);

                              DataColumn dc4 = new DataColumn("username", Type.GetType("System.String"));
                              dt.Columns.Add(dc4);
                              DataRow newRow4;
                              newRow4 = ds.Tables["connectionsettings"].NewRow();
                              newRow4["username"] = txtUsername.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow4);

                              DataColumn dc5 = new DataColumn("password", Type.GetType("System.String"));
                              dt.Columns.Add(dc5);
                              DataRow newRow5;
                              newRow5 = ds.Tables["connectionsettings"].NewRow();
                              newRow5["password"] = txtPassword.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow5);

                              DataColumn dc6 = new DataColumn("option", Type.GetType("System.String"));
                              dt.Columns.Add(dc6);
                              DataRow newRow6;
                              newRow6 = ds.Tables["connectionsettings"].NewRow();
                              newRow6["option"] = txtOption.Text;
                              ds.Tables["connectionsettings"].Rows.Add(newRow6);

                              ds.AcceptChanges();
                              ds.WriteXml(filepath);

                              MessageBox.Show(this, "Databasen har sparats i inställningarna");
                        }
                        catch (System.ArgumentException ae)
                        {
                              MessageBox.Show(this, ae.Message);
                        }

                  }

Is there a way to get the results that I want from writing DataSets/DataTables with ReadXML/WriteXML? Or shall I just keep to that ugly, messy output?

Please help me.

(Oh and by the way.. the <settings></settings> is created when the application is first run on a client.)
Avatar of jonvaughan
jonvaughan

I think this may be happening because you have 2 tables in the dataset with no relation.
Where are the <usersettings> coming from the original file ?

It may be better for you to use the XmlDataWriter / XmlDataReader to explicitly read and write the XML if you want to control it .
In order to write it you simply need to do a foreach over the table and use the WriteElement with the column values as  the parameters.




 
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mindweaver

ASKER

I LOVE you :) That is exactly what I'm looking for. Great answer with complete solution. You should get 1000pts :)
=) LOL! I dont think my wife would appreciate your comments but I understand where you're coming from.

Cheers mate!