[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DataSet/DataTable to XML. Better output

Posted on 2005-05-08
4
Medium Priority
?
1,468 Views
Last Modified: 2008-02-01
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.)
0
Comment
Question by:mindweaver
  • 2
4 Comments
 
LVL 10

Expert Comment

by:jonvaughan
ID: 13955939
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
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13957081
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
 

Author Comment

by:mindweaver
ID: 13957110
I LOVE you :) That is exactly what I'm looking for. Great answer with complete solution. You should get 1000pts :)
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13957125
=) LOL! I dont think my wife would appreciate your comments but I understand where you're coming from.

Cheers mate!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question