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</da tabase>
<username>USERNAME</userna me>
<password>PASSWORD</passwo rd>
<option>TYPE</option>
</connectionsettings>
</settings>
But I keep getting:
<settings>
<usersettings>
<username>MindWeaver</user name>
</usersettings>
<connectionsettings>
<driver>DRIVERNAME</driver >
</connectionsettings>
<connectionsettings>
<server>SERVERNAME</server >
</connectionsettings>
<connectionsettings>
<database>DATABASENAME</da tabase>
</connectionsettings>
<connectionsettings>
<username>USERNAME</userna me>
</connectionsettings>
<connectionsettings>
<password>PASSWORD</passwo rd>
</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("connectionsetti ngs");
DataColumn dc1 = new DataColumn("driver", Type.GetType("System.Strin g"));
dt.Columns.Add(dc1);
ds.Tables.Add(dt);
DataRow newRow1;
newRow1 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow1["driver"] = txtDriver.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w1);
DataColumn dc2 = new DataColumn("server", Type.GetType("System.Strin g"));
dt.Columns.Add(dc2);
DataRow newRow2;
newRow2 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow2["server"] = txtServer.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w2);
DataColumn dc3 = new DataColumn("database", Type.GetType("System.Strin g"));
dt.Columns.Add(dc3);
DataRow newRow3;
newRow3 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow3["database"] = txtDatabase.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w3);
DataColumn dc4 = new DataColumn("username", Type.GetType("System.Strin g"));
dt.Columns.Add(dc4);
DataRow newRow4;
newRow4 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow4["username"] = txtUsername.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w4);
DataColumn dc5 = new DataColumn("password", Type.GetType("System.Strin g"));
dt.Columns.Add(dc5);
DataRow newRow5;
newRow5 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow5["password"] = txtPassword.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w5);
DataColumn dc6 = new DataColumn("option", Type.GetType("System.Strin g"));
dt.Columns.Add(dc6);
DataRow newRow6;
newRow6 = ds.Tables["connectionsetti ngs"].NewR ow();
newRow6["option"] = txtOption.Text;
ds.Tables["connectionsetti ngs"].Rows .Add(newRo w6);
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.)
<settings>
<connectionsettings>
<driver>DRIVERNAME</driver
<server>SERVERNAME</server
<database>DATABASENAME</da
<username>USERNAME</userna
<password>PASSWORD</passwo
<option>TYPE</option>
</connectionsettings>
</settings>
But I keep getting:
<settings>
<usersettings>
<username>MindWeaver</user
</usersettings>
<connectionsettings>
<driver>DRIVERNAME</driver
</connectionsettings>
<connectionsettings>
<server>SERVERNAME</server
</connectionsettings>
<connectionsettings>
<database>DATABASENAME</da
</connectionsettings>
<connectionsettings>
<username>USERNAME</userna
</connectionsettings>
<connectionsettings>
<password>PASSWORD</passwo
</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("connectionsetti
DataColumn dc1 = new DataColumn("driver", Type.GetType("System.Strin
dt.Columns.Add(dc1);
ds.Tables.Add(dt);
DataRow newRow1;
newRow1 = ds.Tables["connectionsetti
newRow1["driver"] = txtDriver.Text;
ds.Tables["connectionsetti
DataColumn dc2 = new DataColumn("server", Type.GetType("System.Strin
dt.Columns.Add(dc2);
DataRow newRow2;
newRow2 = ds.Tables["connectionsetti
newRow2["server"] = txtServer.Text;
ds.Tables["connectionsetti
DataColumn dc3 = new DataColumn("database", Type.GetType("System.Strin
dt.Columns.Add(dc3);
DataRow newRow3;
newRow3 = ds.Tables["connectionsetti
newRow3["database"] = txtDatabase.Text;
ds.Tables["connectionsetti
DataColumn dc4 = new DataColumn("username", Type.GetType("System.Strin
dt.Columns.Add(dc4);
DataRow newRow4;
newRow4 = ds.Tables["connectionsetti
newRow4["username"] = txtUsername.Text;
ds.Tables["connectionsetti
DataColumn dc5 = new DataColumn("password", Type.GetType("System.Strin
dt.Columns.Add(dc5);
DataRow newRow5;
newRow5 = ds.Tables["connectionsetti
newRow5["password"] = txtPassword.Text;
ds.Tables["connectionsetti
DataColumn dc6 = new DataColumn("option", Type.GetType("System.Strin
dt.Columns.Add(dc6);
DataRow newRow6;
newRow6 = ds.Tables["connectionsetti
newRow6["option"] = txtOption.Text;
ds.Tables["connectionsetti
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.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
Cheers mate!
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.