tags:

views:

20

answers:

2

I am using sql databse to save data of a simple note taking application, using dataset and gui is binded with database. Simple work. Using SQL is useless here, i want to save the data to a simple XML file instead of SQL using the same dataset.

I am using Visual Studio 2010 and programming in C# .Net 4.0

+1  A: 

You didn't specify your programming environment. Assuming you're using .NET....

Use the WriteXml method of the dataSet.

There's an article here: http://msdn.microsoft.com/en-us/library/ms233698%28VS.80%29.aspx

David Stratton
I am using Visual Studio 2010 and programming in C# .Net 4.0
LifeH2O
+2  A: 

A dataset of a single table to XML

private void SingleTableToXml()
{
    DataSet myDS = getDataSet();

    // To write out the contents of the DataSet as XML,
    // use a file name to call the WriteXml method of the DataSet class
    myDS.WriteXml(Server.MapPath("filename.xml"), XmlWriteMode.IgnoreSchema);
}

if you have more than one table in the dataset, say it a master-detail relationship, then the method is exactly the same. Just make sure to create the DataRelation between tables and set the relation Nested property to true as in the following code

//Get the primary key column from the master table
DataColumn primarykey = myDS.Tables["Categories"].Columns["CategoryID"];
//Get the foreign key column from the detail table
DataColumn foreignkey = myDS.Tables["Products"].Columns["CategoryID"];

//Assign a relation
DataRelation relation = myDS.Relations.Add(primarykey, foreignkey);

//Ask ADO.NET to generate nested XML nodes
relation.Nested = true;

Hope it helps

Lorenzo
ok, written to XML how will i load from it next time? ReadXml?
LifeH2O
Thats working. Thanks!
LifeH2O
Yes. ReadXml is your way. You can find examples for every override on the [MSDN reference page](http://bit.ly/a53DOL). Good luck!
Lorenzo