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.)
mindweaverAsked:
Who is Participating?
 
b1xml2Commented:
it's simpler than you think:

DataTable dt = new DataTable("connectionsettings");

dt.Columns.Add("driver",typeof(string));
dt.Columns.Add("server",typeof(string));
dt.Columns.Add("database",typeof(string));
dt.Columns.Add("username",typeof(string));
dt.Columns.Add("password",typeof(string));
dt.Columns.Add("option",typeof(string));

ds.Tables.Add(dt);

DataRow row = dt.NewRow();
row["driver"] = txtDriver.Text;
row["server"] = txtServer.Text;
row["database"] = txtDatabase.Text;
row["username"] = txtUsername.Text;
row["password"] = txtPassword.Text;
row["option"] = txtOption.Text;

dt.Rows.Add(row);

ds.AcceptChanges();
ds.WriteXml(filepath);
0
 
jonvaughanCommented:
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.




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

Cheers mate!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.